Ruby 中的相同方法和常量名称
Same method and constant name in Ruby
module Demo
Myconstant = 'This is the constant'
private
def Myconstant
puts 'This is the method'
end
end
class Sample
include Demo
def test
puts Myconstant # => 'This is the constant'
end
end
Sample.new.test
上面的代码片段是如何工作的?
该方法不应该 Myconstant
覆盖 "true" 常量吗?
有没有办法调用该方法?
谢谢。
使用括号显式调用方法:
puts Myconstant
#⇒ 'This is the constant'
puts Myconstant()
#⇒ 'This is the method'
module Demo
Myconstant = 'This is the constant'
private
def Myconstant
puts 'This is the method'
end
end
class Sample
include Demo
def test
puts Myconstant # => 'This is the constant'
end
end
Sample.new.test
上面的代码片段是如何工作的?
该方法不应该 Myconstant
覆盖 "true" 常量吗?
有没有办法调用该方法?
谢谢。
使用括号显式调用方法:
puts Myconstant
#⇒ 'This is the constant'
puts Myconstant()
#⇒ 'This is the method'