HM1
HM1

Reputation: 1684

"RVM is not a function" error when running in ruby script and irb

I get an error when I run %x{rvm use @myapp} in ruby and irb. The error is "RVM is not a function, selecting rubies with 'rvm use ...' will not work".

Here's what I've tried:
1. the "rvm use @myapp" command works in OSX command prompt (using OSX Mavericks)
2. made sure RVM is the latest version.
3. reloaded RVM check RVM is a function in the command prompt
4. (still fails in irb and ruby's %x{})
5. According to some SO posts, I changed OSX terminal preferences from login shell to /bin/bash and /bin/bash --login. Quit, opened new terminal windows but all efforts were in vain.
6. checked .bash_profile for [[-s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Any ideas on how I can get %x{rvm use @myapp} to work in ruby and irb?

Upvotes: 0

Views: 350

Answers (2)

mpapis
mpapis

Reputation: 53188

What happens here is that the shell you had started ruby or irb with had rvm defined both as a function and added to PATH the function takes precedence in shell and it all worked fine, but when you open ruby or irb it is a new process and it inherits only environment variables which includes PATH and does not inherit functions, additionally running %x{} from ruby creates another shell process which is neither a login or interactive shell, and they respectively would make shell load ~/.bash_profile and ~/.bashrc.

Depending on what do you want to do you have few options, to execute another ruby/gem you can use rvm ... do ... for from %x{} like this:

rvm @myapp do ruby -e '...'

OR:

rvm @myapp do gem install ...

OR:

rvm @myapp do bundle install

it allows single command to run in the context of given ruby

Upvotes: 1

Малъ Скрылевъ
Малъ Скрылевъ

Reputation: 16514

Try this trick:

%x{bash -c 'source "$HOME/.rvm/scripts/rvm"; rvm use @myapp'}

However, you really can use rvm as you've specified because even if you've set up the rvm you then lost your session because your terminal will be closed. Try to setup your environment with session gem, and control bash session with it.

require 'session'

@myapp = 'ruby-1.8.7-p374'

bash = Session::Bash.new

stdout, stderr = bash.execute 'source "$HOME/.rvm/scripts/rvm"'

stdout, stderr = bash.execute "rvm use #{@myapp}"
puts stdout

# => Using /home/malo/.rvm/gems/ruby-1.8.7-p374

Upvotes: 0

Related Questions