Question

I'm not sure if this belongs here or somewhere else (SuperUser?) but anyway:

I've got two Ruby scripts, one which requires Ruby 2.0 (A) and another which requires 1.8 (B). A needs to call B with a forked processes. A is something like this:

require "fileutils"
require "json"

...

`name_of_B`

B is an executable script with a shebang, starting like this:

#!/Users/user_name/.rvm/rubies/ruby-1.8.7-p374/bin/ruby
require 'rubygems'
require 'json'

...

I use RVM to manage my Ruby versions:

> rvm list
rvm rubies

   ruby-1.8.7-p374 [ i686 ]
   ruby-1.9.3-p448 [ x86_64 ]
=* ruby-2.0.0-p247 [ x86_64 ]

I run A with:

> ruby name_of_A

but end up with:

/Users/jacobevelyn/.rvm/gems/ruby-2.0.0-p247/gems/json-1.8.1/lib/json/ext/parser.bundle: [BUG] Segmentation fault
ruby 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin12.5.0]

Any thoughts on what I can do? I don't know a whole lot about gems but it appears that B tries to look at gems installed under Ruby 2.0, rather than 1.8. (Yes, I've run gem install json under 1.8 already.) Obviously the scripts are more complicated than they appear here and absolutely cannot be ported or combined (this doesn't mean I don't want to, it means I can't for my use case), otherwise I would.

Was it helpful?

Solution

you need to change the shebang to:

#!/Users/user_name/.rvm/wrappers/ruby-1.8.7-p374/ruby

it will not only use that ruby but also its gems.

in case you use bundler (Gemfile) you might also need to wrap the command invocation in:

Bundler.with_clean_env do
  ...
end

which will reset loaded bundler environment

OTHER TIPS

Call:

result = `\path\to\ruby_1_8 \path\to\ruby_1_8_script.rb`

This will use the correct ruby binary to execute the script that expects it. The result is saved into the var.


You can call which ruby to find the version of the ruby in your current directory. Go to your project / source dir and call it to see the version (presumably Ruby 2) that you're using for the main app. Then, go to your old project / repo (associated with the 1.8 script) and run it again. Hopefully that will show you the path to Ruby 1.8. If not, try it from root (/). Or use RVM to confidently switch to Ruby 1.8 and then call it there to get the path.


I've never used RVM much. If it is confused, and filters things through the wrong gem set, etc, then you may need instead to switch to rbenv. Also, you may need to use its own functions to display the true path to the Ruby 1.8 binary (i.e. maybe it messes with which?) Again, I don't RVM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top