Tag: instance methods

在不实例化类的情况下调用ruby方法

如果我在rails活动模型方法上调用方法,如下所示: class Foo < ActiveRecord::Base end Foo.first 我会回到第一个活跃的记录。 我不必实例化该类。 但是如果我创建自己的类并调用方法,我会得到一个exception: class Person < ActiveRecord::Base def greeting 'hello' end end Person.greeting #EXCEPTION: undefined method `greeting' for Person:Class 我怎么能让这个问题消失呢?