Calling ruby method without instantiating class

Rimian picture Rimian · Oct 25, 2012 · Viewed 10.3k times · Source

If I call a method on a rails active model method like so:

class Foo < ActiveRecord::Base

end

Foo.first

I'll get back the first active record. I don't have to instantiate the class.

But if I create my own class and call a method, I get an exception:

class Person < ActiveRecord::Base
  def greeting
    'hello'
  end
end

Person.greeting 

#EXCEPTION: undefined method `greeting' for Person:Class

How can I make that problem go away?

Answer

Sergio Tulentsev picture Sergio Tulentsev · Oct 25, 2012

There are several kinds of methods. The two most important ones are: instance methods and class instance methods.

Foo.first is a class instance method. It works on a class instance (Foo, in this case). If it stores some data inside the class, that data is shared globally across your program (because there's only one class with name Foo (or ::Foo, to be exact)).

But your greeting method is an instance method, it requires object instance. If your greeting method will use Person's name, for example, it has to be instance method, so that it will be able to use instance data (the name). If it doesn't use any instance-specific state and you really meant it to be a class instance method, then use the self "prefix".

class Person < ActiveRecord::Base
  def self.greeting
    'hello'
  end
end