To use self. or not.. in Rails

Spread the love

Question Description

I’ve been coding in Ruby for sometime now, but I don’t understand when to use:

def self.METHOD_NAME
end

or just:

def METHOD_NAME
end

In any Rails model. Is “self” a modifier like private in Java? When should I use it and when not?. Thanks a ton.

Practice As Follows

def self.method_name
end

defines a class method.

def method_name
end

defines an instance method.

This is a pretty good post on it.

You may be interested in these books.

#ad

As an Amazon Associate I earn from qualifying purchases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.