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 R开发者_运维百科uby 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.
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.io/
This allows you to easily switch and maintain multiple versions of ruby on your system.
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"
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
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"
精彩评论