'How do I get awesome_print to work without using ~/.irbrc file?

I want to use awesome print without putting it in my rails 5 app. Just in the console. The documentation for requiring it in irb is not working.



Solution 1:[1]

That's because bundler isolates the gems available to load to what's in your Gemfile.

The best way to get around this is to add the gem to your Gemfile

gem 'awesome_print', require: false, group: :development

And in your .irbrc, you can require it, so that it is only enabled for you:

begin
  require 'awesome_print'
rescue LoadError => err
  warn "could not require awesome_print: #{err}"
end

However, if you aren't permitted to add awesome_print to your repository for whatever reason, there are a few hacks to get gems installed, but not in your Gemfile to load in this GitHub Gist.

One such example that could be placed at the top of your .irbrc:

# Add all gems in the global gemset to the $LOAD_PATH so they can be used even
# in places like 'rails console'.
if defined?(::Bundler)
  global_gemset = ENV['GEM_PATH'].split(':').grep(/ruby.*@global/).first
  $LOAD_PATH.concat(Dir.glob("#{global_gemset}/gems/*/lib")) if 
  global_gemset
end

Solution 2:[2]

cd your/rails/project
irb

inside irb, run:

require 'awesome_print'
require './config/environment'

and you have both rails console and awesome_print gem while the gem is installed outside of the bundler.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Unixmonkey
Solution 2 Peter