Ruby:require 与 require_relative - 在 Ruby <1.9.2 和 >=1.9.2 中运行的最佳实践 [英] Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

查看:74
本文介绍了Ruby:require 与 require_relative - 在 Ruby <1.9.2 和 >=1.9.2 中运行的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想 require Ruby 中的相对文件并且我希望它在 1.8.x 和 >=1.9.2 中都能工作,那么最佳实践是什么?

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?

我看到了几个选项:

  • 只需执行 $LOAD_PATH <<<'.' 忘记一切
  • do $LOAD_PATH <<File.dirname(__FILE__)
  • 需要'./path/to/file'
  • 检查是否RUBY_VERSION <1.9.2,然后将require_relative定义为require,之后在需要的地方使用require_relative
  • 检查 require_relative 是否已经存在,如果存在,尝试像之前的情况一样进行
  • 使用奇怪的结构,例如
  • just do $LOAD_PATH << '.' and forget everything
  • do $LOAD_PATH << File.dirname(__FILE__)
  • require './path/to/file'
  • check if RUBY_VERSION < 1.9.2, then define require_relative as require, use require_relative everywhere where it's needed afterwards
  • check if require_relative already exists, if it does, try to proceed as in previous case
  • use weird constructions such as
require File.join(File.dirname(__FILE__), 'path/to/file')

-唉,它们似乎不能完全在 Ruby 1.9 中工作,因为,例如:

- alas they don't seem to work in Ruby 1.9 throughly, because, for example:

$ cat caller.rb
require File.join(File.dirname(__FILE__), 'path/to/file')
$ cat path/to/file.rb
puts 'Some testing'
$ ruby caller
Some testing
$ pwd
/tmp
$ ruby /tmp/caller
Some testing
$ ruby tmp/caller
tmp/caller.rb:1:in 'require': no such file to load -- tmp/path/to/file (LoadError)
    from tmp/caller.rb:1:in '<main>'

  • 更奇怪的构造:

  • Even weirder construction:

    require File.join(File.expand_path(File.dirname(__FILE__)), 'path/to/file')

    似乎有效,但它很奇怪而且不太好看.

  • 使用 backports gem - 它有点笨重,它需要 ruby​​gems 基础设施并包括大量其他解决方法,而我只是想让 require 处理相关文件.
  • seems to work, but it's weird and not quite good looking.

  • Use backports gem - it's kind of heavy, it requires rubygems infrastructure and includes tons of other workarounds, while I just want require to work with relative files.
  • StackOverflow 上有一个密切相关的问题这提供了更多示例,但没有给出明确的答案 - 这是最佳做法.

    There's a closely related question at StackOverflow that gives some more examples, but it doesn't give a clear answer - which is a best practice.

    是否有任何体面的、被所有人接受的通用解决方案让我的应用程序在 Ruby <1.9.2 和 >=1.9.2 上运行?

    Is there are any decent, accepted-by-everyone universal solution to make my application run on both Ruby <1.9.2 and >=1.9.2?

    澄清:我不想要像你可以做 X"这样的答案——事实上,我已经提到了大部分有问题的选择.我想要基本原理,即为什么这是最佳实践,它的优缺点是什么以及为什么应该在其他实践中选择它.

    Clarification: I don't want just answers like "you can do X" - in fact, I've already mentioned most of choices in question. I want rationale, i.e. why it is a best practice, what are its pros and cons and why it should be chosen among the others.

    推荐答案

    一个解决方法刚刚添加到 'aws' gem 中,所以我想我会分享它,因为它受到这篇文章的启发.

    A workaround for this was just added to the 'aws' gem so thought I'd share as it was inspired by this post.

    https://github.com/appoxy/aws/blob/master/lib/awsbase/require_relative.rb

    unless Kernel.respond_to?(:require_relative)
      module Kernel
        def require_relative(path)
          require File.join(File.dirname(caller[0]), path.to_str)
        end
      end
    end
    

    这允许您像在 ruby​​ 1.9.2 和 ruby​​ 1.8 和 1.9.1 中一样使用 require_relative.

    This allows you to use require_relative as you would in ruby 1.9.2 in ruby 1.8 and 1.9.1.

    这篇关于Ruby:require 与 require_relative - 在 Ruby &lt;1.9.2 和 &gt;=1.9.2 中运行的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆