2011-01-31 34 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

相关问题