2011-01-31 37 views

回答

3

使用雙冒號創建一個類的方法。

class A 
    def A::f 
    puts "Called A::f" 
    end 
end 

puts "A.public_methods:" 
puts A.public_methods(false) 
puts 

a = A.new 

puts "a.public_methods:" 
puts a.public_methods(false) 
puts 

puts "Attempt to call f" 
A.f 
a.f 

輸出:你可以做一個簡單的測試,看看這

A.public_methods: 
f 
allocate 
new 
superclass 

a.public_methods: 

Attempt to call f 
Called A::f 
NoMethodError: undefined method ‘f’ for #<A:0x0000010084f020> 
+0

謝謝,但它似乎不可思議使用::定義一個類的方法... – Rasefon 2011-01-31 09:20:38

相關問題