Ruby: require vs require_relative - best practice to workaround running in both Ruby <1>=1.9.2

后端 未结 11 1867
臣服心动
臣服心动 2020-11-27 08:50

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:

11条回答
  •  误落风尘
    2020-11-27 09:40

    The backports gem now allows individual loading of backports.

    You could then simply:

    require 'backports/1.9.1/kernel/require_relative'
    # => Now require_relative works for all versions of Ruby
    

    This require will not affect newer versions, nor will it update any other builtin methods.

提交回复
热议问题