调用类的方法在Ruby中,你怎么骂从该类的实例中的一个类的方法?假设我有红宝石:从实例
class Truck
def self.default_make
# Class method.
"mac"
end
def initialize
# Instance method.
Truck.default_make # gets the default via the class's method.
# But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
end
end
行Truck.default_make
检索默认值。但是有没有一种方式说这个,而不提Truck
?似乎应该有。
我想看到一些ruby中的快捷方式来调用一个实例的类方法。即:> some_class_method,而不是self.class.some_class_method – phoet 2011-12-28 11:22:57
,虽然这是正确的答案,但很遗憾“self.class”比班级名称“Truck”更易打字并且不易读。哦,好吧...... – 2012-01-11 11:18:38
@MattConnolly,它是相对的,如果你的类名是'SalesforceSyncJob',那么它就更短了;) – drewish 2013-02-27 21:00:40