# Why use self. for a method?

**URL:** <https://www.ruby-forum.com/t/why-use-self-for-a-method/132947>\
**Category:** Rails\
**Created:** [March 26, 2008, 1:42am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947 "2008-03-26T01:42:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 1:42am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947/1 "2008-03-26T01:42:05Z")

</div>

hi all.  
I had a problem with a methods and classes  
what i had for example was this:

the controller:  
class AnimalsController \< ApplicationController  
def index  
@id =Animal.item  
end  
end

the model:  
class Animal  
def item  
return 2  
end  
end

this doesn’t work because you have to maken from the Item Method a Class  
Method.  
you do this by doing def self.item.

But why ? and if you have mutiple methods in it you will have to set  
them all to self. ??

jeljer

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 3:03am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947/2 "2008-03-26T03:03:32Z")

</div>

Heldop S. wrote:

> hi all.  
> I had a problem with a methods and classes  
> what i had for example was this:
> 
> the controller:  
> class AnimalsController \< ApplicationController  
> def index  
> @id =Animal.item  
> end  
> end
> 
> the model:  
> class Animal  
> def item  
> return 2  
> end  
> end
> 
> this doesn’t work because you have to maken from the Item Method a Class  
> Method.  
> you do this by doing def self.item.
> 
> But why ? and if you have mutiple methods in it you will have to set  
> them all to self. ??
> 
> jeljer

You are trying to call that method as a ‘class’ level method.

Adding self at the start declares a method as a class method, which is  
why it then worked.

You probably want to be instantiating the object, so you would say

def index  
animal = Animal.new;  
@id = animal.item;  
end

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 7:17am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947/3 "2008-03-26T07:17:55Z")

</div>

What is the difference between a “method” inside the class ModelName \<  
ActiveRecord::Base and a “class model” inside the same model\* beside  
having to use “self.” to make it work?

\*the model is a class isn’t it?

On Mar 25, 11:03 pm, Dave F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 7:19am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947/4 "2008-03-26T07:19:42Z")

</div>

Correction: I want to know the difference between a “method” and a  
“class method” inside a model (which is a class, isn’t it?)

---

<div class="post-metadata">

**Author:** ![heldopslippers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@heldopslippers](https://www.ruby-forum.com/u/heldopslippers)\
**Post date:** [March 26, 2008, 8:26am UTC](https://www.ruby-forum.com/t/why-use-self-for-a-method/132947/5 "2008-03-26T08:26:48Z")

</div>

On 26 Mar 2008, at 06:19, Daniel D. wrote:

> Correction: I want to know the difference between a “method” and a  
> “class method” inside a model (which is a class, isn’t it?)

When you have an instance method, you need an instance of the object.  
For example if I have a Person class, and Person has a last\_name  
attribute it makes no sense to say Person.last\_name, the class itself  
doesn’t have a last name, only instances of Person do. If bob is an  
instance of Person, then you can say bob.last\_name and that makes  
perfect sense.  
With a class method you don’t need an instance of the class. For  
example if Person is an ActiveRecord class then you have a class  
method called find, so you can say Person.find (and writing bob.find  
would be a bit weird).

The self. notation is because a class method is actually a singleton  
method on the Class. All of your classes are instances of Class,  
things like find and so on are singleton methods on those instances of  
class. Equvalently you can write

class Foo  
class \<\< self  
def a\_class\_method  
end

```
def another_one
end

```

end  
end

Fred
