2013-04-06 31 views
1
獲取類名和方法名

說我有,在Ruby中

class Foo 
    def self.bar 
    puts "I want to print #{some_method_for_class_name} and #{some_method_for_method_name}." 
    end 
end 

有沒有辦法讓「富」,並分別some_method_for_class_namesome_method_for_method_name的地方「吧」?

回答

2

像這樣的東西應該工作:

class Foo 
    def self.bar 
    puts "I want to print #{self} and #{__method__}." 
    end 
end 

如果不是你需要使用self.class而是一個類的方法。