0

所以我有一个 Ruby 方法和一个模块内部的常量:

module Foo
  URL = "http://foo.bar"

  def self.fetch
    # how can I get Foo::URL from in here?
  end
end

我如何Foo::URL从里面得到Foo.fetch

4

2 回答 2

2

您无需指定模块,因为您在模块中。因此,

module Foo
  URL = "http://foo.bar"

  def self.fetch
     URL
  end
end

这样,

Foo.fetch 
# => "http://foo.bar"
于 2013-09-14T01:21:00.647 回答
1

您应该可以通过 Foo::URL 访问。这对我有用:

module Foo
  URL = "BAR"
  def self.baz
    Foo::URL
  end
end

2.0.0-p195 :025 > Foo.baz
=> "BAR"

您还应该可以URL从模块内部访问裸机。当您尝试访问时遇到什么错误Foo::URL

于 2013-09-14T01:29:31.847 回答