9

I am using rails 4.1 and ruby 2.1.1

Everything works, but when I run rails console I get this error

> rails console
Loading development environment (Rails 4.1.0)
load error: /home/andreas/.rvm/rubies/ruby-2.1.1/.irbrc
NoMethodError: undefined method `split' for nil:NilClass
    /home/andreas/.rvm/scripts/irbrc.rb:41:in `<top (required)>'

After the error the console opens and can be used.

Here's the 41th line and surroundings in the .irbrc file.

39 # Calculate the ruby string.
40 rvm_ruby_string = ENV["rvm_ruby_string"] ||
41 (ENV['GEM_HOME'] && ENV['GEM_HOME'].split(/\//).last.split(/@/).first) ||
42 ("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" rescue nil) ||
43 (RUBY_DESCRIPTION.split(" ")[1].sub('p', '-p') rescue nil ) ||
44 (`ruby -v` || '').split(" ")[1].sub('p', '-p')

I get these results when testing in the console

irb(main):008:0> ENV['GEM_HOME']
=> ""
irb(main):009:0> ENV['GEM_HOME'].split(/\//).last
=> nil

If I run irb outside the rails application, I get

2.1.1 :001 > ENV['GEM_HOME']
 => "/home/andreas/.rvm/gems/ruby-2.1.1" 
2.1.1 :002 > ENV['GEM_HOME'].split(/\//).last
 => "ruby-2.1.1" 

Do you know why the environment variable is blank in the rails application?

6
  • Can you give us more details about your stack? I take you're running on a flavour of Linux -- how is it setup? Commented Apr 22, 2014 at 10:31
  • I've seen this too - running bundle exec rails c produced the error but not bin/rails c. I've not had a chance to dig further. Perhaps bundler messing with the gem environment in a way that confuses rvm? Commented Apr 22, 2014 at 10:41
  • 1
    @RichPeck It's newly updated ubuntu 14.04, rails 4.1 and ruby 2.1.1. All was updated from the previous version. Commented Apr 22, 2014 at 12:54
  • @FrederickCheung I get the error with all possible command (bin/rails , bundle exec rails c and rails c). I am guessing that rails GEM_HOME can be different than the global GEM_HOME, and when rails c is run the rails GEM_HOME is in use. Commented Apr 22, 2014 at 13:06
  • This is probably because something happened with your rvm environment. Something just happened with mine so I am reinstalling rvm since I had 10GB of out of date ruby versions on there anyway. Commented Mar 5, 2015 at 6:08

4 Answers 4

9

If you encounter this problem you should restart you computer. If that does not fix it read on.

The bin/spring file sets ENV["GEM_HOME"] to a blank string

bin/spring

11 ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
12 ENV["GEM_HOME"] = ""
13 Gem.paths = ENV

This crashes when running rails console because in line 41

ENV['GEM_HOME'].split(/\//).last

returns nil if ENV['GEM_HOME'] is blank

~/.rvm/rubies/ruby-2.1.1/.irbrc

39 # Calculate the ruby string.
40 rvm_ruby_string = ENV["rvm_ruby_string"] ||
41 (ENV['GEM_HOME'] && ENV['GEM_HOME'].split(/\//).last.split(/@/).first) ||
42 ("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" rescue nil) ||
43 (RUBY_DESCRIPTION.split(" ")[1].sub('p', '-p') rescue nil ) ||
44 (`ruby -v` || '').split(" ")[1].sub('p', '-p')

rvm uses the string to set the prompt message in the console. If you change line 12 in bin/spring to

ENV["GEM_HOME"] = "Spring is great!"

You get this nice prompt

bin/rails c
Loading development environment (Rails 4.1.0)
Spring is great! :001 > 

I don't really understand why ENV["GEM_HOME"] is set to a blank string. So, I just change this to get rid of the error. I have posted an issue on the spring github page.

Beware!

Any changes to the bin/spring file gets overwritten when you run the spring binstub command

Sign up to request clarification or add additional context in comments.

Comments

2

I just encountered the same problem, and I solved it by spring stop. I think there is no need to restart the computer. What you should do is to restart spring.

1 Comment

In my case I had to restart the computer I tried spring stop, but I still had the error
0

These steps solved my problem:

  • find the current ruby version

ruby -v

example:

ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

  • remove this version

rvm remove 2.1.2

  • install again

rvm install 2.1.2

Comments

0

You may just need to specify a gemset with

rvm gemset use YourGemset

I find it's a good idea to use a separate gemset for each project. Then place the name of that project's gemset in a file named .ruby-gemset in the root of your project. See https://rvm.io/workflow/projects for more info.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.