0

我们正在为一个新项目使用 ruby​​ 方面,我需要“constantize”方法的等价物。我知道我可以为此需要 ActiveSupport,但我想知道,这是 Factes 提供的东西还是我需要 ActiveSupport。

4

1 回答 1

2

这不会回答您问题的“Facets 提供的东西”部分,但它肯定回答了“或者我是否需要 ActiveSupport”这个问题。它还向您展示了该方法的实际实现方式,这非常简单

class String
  def constantize
    self.split("::").inject(Module) {|acc, val| acc.const_get(val)}
  end
end

编辑:对于它的价值,Facets 有一个constant方法,请参阅http://rubyworks.github.com/rubyfaux/?doc=http://rubyworks.github.com/facets/docs/facets-2.9.3/core .json#api-module-Kernel/api-method-Kernel-h-constant

于 2012-02-27T10:22:33.827 回答