1

I'd like the option to use awesome_print in every IRB console or Rails console.

The IRB console is pretty much working satisfactorily right now. If I run irb, I can type require 'awesome_print' and it works.

The Rails console isn't as easy. require 'awesome_print' doesn't work. I apparently have to do this:

> $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'

After that, require 'awesome_print' works fine.

But I definitely don't want to have to type $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib' and then require 'awesome_print' every single time I open a Rails console just to be able to use awesome_print. That seems ridiculous.

So, how can I permanently add a path to Ruby's $LOAD_PATH?

Note: I don't want to add awesome_print to the Gemfile of any particular project. I want awesome_print to be available to all my Ruby/Rails projects.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

2 Answers2

1

You could simply use a a ~/.irbrc file and do:

require 'awesome_print'

Now, open up another IRB prompt:

irb(main):003:0> ap hash
{
    "a" => "b"
}

Edit: this isn't working in rails, seems to be a known issue.

Anthony
  • 15,435
  • 4
  • 39
  • 69
  • it will depend on your version of ruby that you'r invoking `irb` with. if your gem isn't installed for that version of ruby, it will fail. You can always check it with `ruby ~/.irbrc` to see if you get any warnings. The rails issue is known. – Anthony Feb 01 '17 at 16:47
  • Sorry, it does in fact work for me in IRB. I don't know why it didn't work initially but now it does. – Jason Swett Feb 01 '17 at 16:49
  • The issue you linked to seems to be an issue of Rails ignoring `~/.irbrc`. I don't have that problem. Rails picks up `~/.irbrc` fine, it just says `cannot load such file -- awesome_print`, presumably because it's lacking the right path in `$LOAD_PATH`. – Jason Swett Feb 01 '17 at 16:50
  • interesting, i couldn't get it to pick up my irbrc file. Same version of ruby in your rails app? you could `puts ruby -v` at the top of your ~/.irbrc file to make sure. – Anthony Feb 01 '17 at 22:24
0

puts the following to the .irbrc:

to_load = %w[
  awesome_print
  coderay
  hirb
  pry
  pry-doc
  pry-remote
  pry-theme
  slop
  yard
].join('|')

regexp = Regexp.new( "(#{to_load})" )

Gem.path.each do |path|
  Dir.new("#{path}/gems").each do |gem_path|
    next if %w[ . .. ].any?{ |d| gem_path == d }

    new_el = "#{path}/gems/#{gem_path}/lib"
    $LOAD_PATH << new_el if new_el =~ regexp
  end
end
sumskyi
  • 1,827
  • 13
  • 13