views:

149

answers:

4
+4  Q: 

Force Ruby Version

I just got burned because I used find_index on an array on my dev box (OSX with Ruby 1.8.7) and the deployment machine runs Ruby 1.8.6. (What's the difference between find_index and index? The latter works on 1.8.7 and 1.8.6)

So that got me thinking: what's the best way to force Rails to run with a specific Ruby version?

Since it's probably relevant (install multiple rubys!), I need to know this for OSX, but it would be useful to know for Linux, Windows, and Commodore 64 as well.

Later: Of course I'm working in a virtual appliance now, but I'd like to be able to control my Ruby versions if possible on my computer.

Note: I don't care too much disallowing Rails running with the wrong Ruby version. I'm more interested in getting the RIGHT ruby version to run. Sorry for the confusion.

+1  A: 

This is brute force and ignorance, but one approach would be

raise "Wrong ruby version, please use ruby 1.8.7" unless RUBY_VERSION == "1.8.7"
Andrew Grimm
Well, that's definitely part of the question, thanks for that. I'm kind of asking about the other side, how to get the right ruby to run.
Yar
+4  A: 

This won't force the version of ruby required but you may want to utilize something like RVM to easily manage your ruby environment on your dev and production boxes.

http://rvm.beginrescueend.com/

This allows you to easily switch and maintain multiple versions of ruby on your system.

Jim Jeffers
Nice, I'll check it out. I don't manage the production boxes, but I'd like to know what's possible.
Yar
Cool, it does OSX too.
Yar
Yes at the very least you can easily adjust your local machine to match your production server. Also - you should look into having your sysadmin upgrade to 1.8.7 as there are a lot of security / performance issues with with 1.8.6
Jim Jeffers
A: 

Use the RUBY_VERSION constant in your Application controller. This shows rendering the 500 error page. You would want to setup a new page in your public dir with an appropriate message.

before_filter :check_ruby_version

def check_ruby_version unless RUBY_VERSION == "1.8.7" render :file => File.join(Rails.public_path, '500.html'), :status => 500 end end

FillSee
Wouldn't you just want to put that in the environment.rb so the app doesn't start in the first place?
Yar
+1  A: 

Another way to look at the problem would be to be able to disregard differences in the version of Ruby you're running. My backports gem brings Ruby 1.8.6 up to date in the 1.8.x line (including the upcoming 1.8.8) and much of 1.9:

require "backports"

Or instead, for the less courageous among us, you can require only 1.8.7's features:

require "backports/1.8.7"
Marc-André Lafortune
+1 sounds scary.
Yar
You can pick and choose, which should make it less scary...
Marc-André Lafortune