0
我想了解爲什麼我需要範圍解析運算符來訪問繼承模塊中的常量,但不是方法。假設:在Ruby中繼承和範圍解決方案
module Foo
SOMETHING = {:one => 1, :two => 2}
def showit
p SOMETHING
end
end
和
class Bar
include Foo
def initialize
# why doesn't method name need scope resolution but const does?
showit
p Foo::SOMETHING
end
而且相關的,你可以訪問實例或酒吧Foo中聲明的局部變量?我試過這樣的事情:
module Foo
@myar = "some string"
myvar2 = "some other string"
def showit
p @myvar
p myvar2
end
end
並從Bar類中調用showit ...打印@myvar導致nil和myvar2未定義。我希望這兩個字符串都能打印......發生了什麼事情?
好吧,我想它並不需要它。 – thelostspore