Can't get awesome_print gem to work

10,902

Solution 1

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

Solution 2

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.

Solution 3

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!

Solution 4

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 ~/.irbrc and ~/.pryrc. 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

# ~/.irbc and ~/.pryrc

$LOAD_PATH << "~/.asdf/installs/ruby/2.6.3/lib/ruby/gems/2.6.0/gems/awesome_print-1.8.0/lib/"
require "awesome_print"
AwesomePrint.irb!

Solution 5

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.

Share:
10,902

Related videos on Youtube

PJP
Author by

PJP

Long time developer in lots of different fields on different hardware using different languages for different reasons. I have years and years of experience with Ruby, Perl, Java, SQL, Pascal, C, various assembly languages and interpreted BASICs. And some Python. "Every piece of computer software, no matter how small, involves at least a team of two — me, and me six months from now when I have to fix it." — Tony Williams from Slashdot.com "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability." — John Woods "According to Larry Wall in "Programming Perl", 2nd Edition, O'Reilly &amp; Associates, 1996, the original author of the Perl programming language, there are three great virtues of a programmer; Laziness, Impatience and Hubris Laziness: The quality that makes you go to great effort to reduce overall energy expenditure. It makes you write labor-saving programs that other people will find useful and document what you wrote so you don't have to answer so many questions about it. Impatience: The anger you feel when the computer is being lazy. This makes you write programs that don't just react to your needs, but actually anticipate them. Or at least pretend to. Hubris: The quality that makes you write (and maintain) programs that other people won't want to say bad things about." — Three Virtues

Updated on September 16, 2022

Comments

  • PJP
    PJP about 1 year

    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?

    • jvnill
      jvnill over 10 years
      you need to add it to your Gemfile
  • Green
    Green over 10 years
    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.
  • Dave Munger
    Dave Munger over 8 years
    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.
  • Jan Steinman
    Jan Steinman almost 3 years
    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.
  • csebryam
    csebryam almost 3 years
    Make sure you check your ruby version with ruby -v. Also, I've been using pry , gem install pry for syntax highlight instead.