所以我有一个 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
?
您无需指定模块,因为您在模块中。因此,
module Foo
URL = "http://foo.bar"
def self.fetch
URL
end
end
这样,
Foo.fetch
# => "http://foo.bar"
您应该可以通过 Foo::URL 访问。这对我有用:
module Foo
URL = "BAR"
def self.baz
Foo::URL
end
end
2.0.0-p195 :025 > Foo.baz
=> "BAR"
您还应该可以URL
从模块内部访问裸机。当您尝试访问时遇到什么错误Foo::URL
?