Last Updated: February 25, 2016
·
1.282K
· mchung

Write once, run (Ruby) anywhere

As Ruby developers, there are times when we might need to run bits of code across multiple Ruby runtimes.

Perhaps you're interested in the performance improvements in a different version of Ruby, or perhaps you're trying to hunt down a regression, or maybe it's because you're doing the Rails 2 to Rails 3 upgrade dance.

If you have RVM available, there's a built-in function which helps out with this:

$ echo "puts ENV['RUBY_VERSION']" > script.rb
$ rvm '1.9.2-p320, 1.9.3-p327' do ruby script.rb 
ruby-1.9.2-p320
ruby-1.9.3-p327

You can make this easy on the fingers by adding the following bash function to your environment.

function allruby() {
    rvm '1.9.2-p320@app, 1.9.3-p327@app' do ruby $@
}
export -f allruby

And now you can type:

$ allruby script.rb

The $@ at the end of bash function holds the remaining command-line parameters, passing it along to your Ruby program.

$ echo 'puts "#{ENV['RUBY_VERSION']} says #{ARGV}"' > script.rb
$ allruby script.rb hello world
ruby-1.9.2-p320 says ["hello", "world"]
ruby-1.9.3-p327 says ["hello", "world"]

1 Response
Add your response

If you happen to use rbenv, there's a similar plugin available from Chris Eppstein at https://github.com/chriseppstein/rbenv-each

Then you can run

rbenv each -v ruby -e 'puts "hello, world"'

And it outputs

[1.9.3-p374]: ruby -e puts "hello, world"
*******************************************************
hello, world

[1.9.3-p385]: ruby -e puts "hello, world"
*******************************************************
hello, world

[jruby-1.7.2]: ruby -e puts "hello, world"
*******************************************************
hello, world
over 1 year ago ·