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:
The Pickaxe has a snippet for this for 1.8. Here it is:
def require_relative(relative_feature)
c = caller.first
fail "Can't parse #{c}" unless c.rindex(/:\d+(:in `.*')?$/)
file = $`
if /\A\((.*)\)/ =~ file # eval, etc.
raise LoadError, "require_relative is called in #{$1}"
end
absolute = File.expand_path(relative_feature, File.dirname(file))
require absolute
end
It basically just uses what Theo answered, but so you can still use require_relative.