19

awesome_print looks like a pretty nice gem, so I wanted to try it out.

I went to one of my projects and did:

gem install awesome_print

and it says one gem installed, documentation installed, etc.

Then, while I am in that project, I went to my Rails console to try it out, but when I did a require "awesome_print" as their help file says, I get a "cannot load such file".

Has anyone got this to work?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 5
    you need to add it to your Gemfile – jvnill Feb 22 '13 at 02:43
  • 1
    so "adding to gem file" is still needed although we do a gem install? –  Feb 22 '13 at 03:01
  • 2
    No, you don't _need_ to add it to your Gemfile. The [selected answer](http://stackoverflow.com/a/15017892/199712) shows a way to use awesome_print without adding it to your Gemfile. I personally don't want to add awesome_print to my Gemfile because a) I want to be able to take advantage of awesome_print for any Ruby project on my system and b) I don't want to add a gem to a particular project's Gemfile that's only relevant to my personal preferences and not actually a requirement for the project to work. – Jason Swett Feb 01 '17 at 16:10

6 Answers6

28

gem install will put the gem code on your computer, but unless the gem's source code files are on your load path, require won't be able to find them. bundle exec looks at the nearest Gemfile.lock and adds the source code for all the gems listed there to your load path. Rails initialization includes getting Bundler to do this for you.

One solution is to add awesome_print to your Gemfile. However, this will cause your application to have awesome_print as a dependency. Alternatively you can manually add the awesome_print library to your load path after starting up the Rails console and then requiring it:

$ rails c
> $LOAD_PATH << path/to/awesome_print-x.x.x/lib
> require 'awesome_print'
> ap {foo: {bar: {baz: :qux}}}

If you're using RVM, the path is likely to be something like:

~/.rvm/rubies/ruby-x.x.x-pxxx@your_gemset_name/gems/awesome_print-x.x.x/lib
user664833
  • 18,397
  • 19
  • 91
  • 140
Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64
  • awesome_print doesn't work if you do `C:\> app` or `C:\> app.request`, `C:\> app.response` and others. It doesn't affect method calls. – Green Jun 01 '13 at 18:25
  • I absolutely have awesome_print in my Gemfile and it installs just fine, but I get the same error as the original OP when I try to require it inside a file. – Dave Munger Feb 07 '15 at 22:17
  • should the load path be in quotes? – J.R. Bob Dobbs Aug 21 '22 at 15:17
27

Add it to your Gemfile like this:

gem 'awesome_print', :require => 'ap'

I add it to the development group, since that's the only time I need it. The gem doesn't have any other gem dependencies, so I routinely add it to my Gemfile.

Also, add these two lines to your ~/.irbrc file to set ap to be your default pager:

require "awesome_print"
AwesomePrint.irb!

Note that if you use this, however, any projects where awesome_print is not installed in its Gemfile will raise this error when you run rails c:

cannot load such file -- awesome_print

Depending on whatever else you may have in your ~/.irbrc file, this can cause other side effects, such as messing up your prompt. To avoid these, simply add the two lines to the very end of that file.

Guillaume
  • 21,685
  • 6
  • 63
  • 95
platforms
  • 2,666
  • 1
  • 18
  • 23
6

install it :

$ gem install awesome_print

include it in you GemFile, if you want :

gem 'awesome_print', :require => 'ap'

add this line to the file ~/.irbrc :

require 'awesome_print'
AwesomePrint.irb!

restart your shell!

just a note: I did this and it didnt work right away, probably need to restart the computer... or I just needed to close all shell tabs and open the terminal again!

Brian Joseph Spinos
  • 2,144
  • 2
  • 18
  • 18
5

Install the gem on your machine

gem install awesome_print

Get the path to which it has installed

gem which awesome_print

Add the following configuration to your ~/.irbrcor ~/.pryrc or ~/.rdbgrc. This will load Awesome Print whenever you fire an IRB or a pry session.

*Remember $LOAD_PATH will hold whatever you got from typing gem which awesome_print

Edit: Adding ; nil to the $LOAD_PATH line will prevent its contents from being printed out every time the file is executed.

# ~/.irbc or ~/.pryrc or ~/.rdbgrc

$LOAD_PATH << "~/.asdf/installs/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/awesome_print-1.8.0/lib/"; nil
require "awesome_print"
AwesomePrint.irb!
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
  • I can ```gem install awesome_print```. I can ```require``` it from irb. But I cannot ```require``` it from my scripts! It was working just a few days ago! And right after installation, ```gem which awesome_print``` fails with "ERROR: Can't find Ruby library file or shared library awesome_print". – Jan Steinman Dec 14 '20 at 20:09
  • This approach worked nicely for me, but the entire $LOAD_PATH was printed out every time I opened my Rails console. Adding `; nil` to the end fixed it for me. Ex. `$LOAD_PATH << "path_to_gem"; nil` – Rocket Appliances Jun 21 '23 at 19:56
  • 1
    @RocketAppliances can you edit my answer? – Kaka Ruto Aug 19 '23 at 11:55
1

If you are looking to install it without having it in your Gemfile, this is how to do it:

$ gem install awesome_print

I was running into an issue where it was installing successfully but it not in the right directory.

In that case just put this in your .bashrc, this will set the load path:

export PATH="/home/user/.gem/ruby/2.3.0/bin:$PATH"
PATH="`ruby -e 'puts Gem.user_dir'`/bin:$PATH"

replace 2.3.0 with the version of ruby you are working with.
replace user with your username or if you are using vagrant then replace with vagrant

reload your .bashrc or exit the Terminal to reload changes, then install the gem again.

csebryam
  • 1,091
  • 11
  • 13
  • Looked hopeful, but I have nothing in ```~/.gem/ruby/2.3.0``` except a ```cache``` directory, and it doesn't have awesome_print in it. – Jan Steinman Dec 14 '20 at 20:13
  • Make sure you check your ruby version with `ruby -v`. Also, I've been using `pry` , `gem install pry` for syntax highlight instead. – csebryam Dec 15 '20 at 23:11
0

In my case, I struggled with PATHs and such, while missing something obvious!

# which ruby
/usr/bin/ruby
# ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
# locate bin/ruby
/usr/bin/ruby
/usr/local/Cellar/ruby/2.7.2/bin/ruby
/usr/local/opt/ruby/bin/ruby
# /usr/local/opt/ruby/bin/ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin17]
#

Aha! Version crud. I was running an old ruby. Thanks, Apple!

# sudo mv /usr/bin/ruby /usr/bin/ruby_2.3.7
# sudo ln /usr/local/opt/ruby/bin/ruby /usr/bin/ruby

Solved the problem!

There is probably something I could have told brew to do to fix things, but I was impatient. :-)

Jan Steinman
  • 325
  • 3
  • 11