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
方法覆盖“真”常数吗?
有没有办法调用该方法呢?
感谢。
答案 0 :(得分:1)
使用括号显式调用方法:
puts Myconstant
#⇒ 'This is the constant'
puts Myconstant()
#⇒ 'This is the method'