What is the best practice if I want to require a relative file in Ruby and I want it to work in both 1.8.x and >=1.9.2?
I see a few options:
I'm a fan of using the rbx-require-relative gem (source). It was originally written for Rubinius, but it also supports MRI 1.8.7 and does nothing in 1.9.2. Requiring a gem is simple, and I don't have to throw code snippets into my project.
Add it to your Gemfile:
gem "rbx-require-relative"
Then require 'require_relative' before you require_relative.
For example, one of my test files looks like this:
require 'rubygems'
require 'bundler/setup'
require 'minitest/autorun'
require 'require_relative'
require_relative '../lib/foo'
This is the cleanest solution out of any of these IMO, and the gem isn't as heavy as backports.