0

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.

CJ Jean
  • 971
  • 1
  • 8
  • 10

2 Answers2

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
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • Thank you Unixmonkey, Forgive my ignorance but I do not know where to find the .irbrc file in the rails 5 app. I am a learning newbie. Is there a way to use it without putting it in the Gemfile? – CJ Jean Aug 10 '17 at 22:37
  • 1
    The .irbc file should go in your users root directory. Then when you have a Rails app with Awesomeprint in the gemfile, it will be used in Rails console. – Bob Roberts Aug 11 '17 at 04:42
0
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.

Peter
  • 2,162
  • 3
  • 21
  • 27