Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling ruby method without instantiating class

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?

like image 449
Rimian Avatar asked Nov 29 '22 08:11

Rimian


1 Answers

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
like image 188
Sergio Tulentsev Avatar answered Dec 14 '22 21:12

Sergio Tulentsev