ruby - 动态需要文件?

标签 ruby dynamic rubygems namespaces require

有没有人足够了解 Ruby 的 require 来告诉我以下语法是否有效:

class Something

  def initialize(mode)
     case mode
     when :one then require 'some_gem'
     when :two then require 'other_gem'
     end
  end

end

s = Something.new

如果是这样,需要将 gem 放入全局命名空间,就像在文件顶部时一样?

最佳答案

If so, would the require place the gem into the global namespace as the same require at the top of the file would?

是的。 require 没有范围,而 load 有。

关于ruby - 动态需要文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2230512/

相关文章:

ruby - 如何查找数组中的部分重复项?

javascript - 使用 JQuery 动态确定点击了哪个链接

c# - 动态创建类到cs文件?

typescript - 使用 typescript 在构造函数中动态设置类属性

Ruby 1.8.6 `gem install` 段错误与 "abort trap 6"

ruby - GitLab 设置问题与 bundle

ruby - 如何在 iPad 模拟器上运行 Cucumber、Ruby 脚本

ruby-on-rails - 如何在rails中迁移dbase数据库

mysql - Gem mysql 连接错误

ruby - 无法通过 Ruby 将字母中的英文单词组合起来