0
我試圖如果我可以通過instances of class
或instances of class
來調用Class instance methods
。因此嘗試了以下:與`類實例方法'混淆
class Foo
def show; p "hi" ; end
def self.display ; p "hello" ; end
end
#=> nil
Foo.display
#"hello"
#=> "hello"
Foo.new.show
#"hi"
#=> "hi"
Foo.show
#NoMethodError: undefined method `show' for Foo:Class
#from (irb):7
#from C:/Ruby200/bin/irb:12:in `<main>'
但是在下面呼叫我期望相同的錯誤NoMethodError:未定義的方法`顯示」。但爲什麼不是這樣呢?
Foo.new.display
#<Foo:0x538020> #=> nil
foo = Foo.new
#=> #<Foo:0x22bc438>
foo.display
#<Foo:0x22bc438> #=> nil
讓我嘗試一些不同的東西。 :) –
是的!你是對的.. OMG! :) –