# Question on self (Newbie)

**URL:** <https://www.ruby-forum.com/t/question-on-self-newbie/59727>\
**Category:** Ruby\
**Created:** [May 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727 "2006-05-05T20:51:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/1 "2006-05-05T20:51:17Z")

</div>

I am working my way through some tutorials and books on Ruby and i am  
trying to understand the various uses of the self object. For example  
this code from Agile rails:

def self.login(name,password)

# code here

end

def try\_to\_login  
User.login(self.name, self.password)  
end

#Both functions belong to a class Named User

Now I understand the self.name and self password reference the calling  
object. However, I am still trying to figure out why in this example the  
function is prefixed with self.

Ben

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/2 "2006-05-05T21:02:59Z")

</div>

On 5/5/06, Benjamin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now I understand the self.name and self password reference the calling  
> object. However, I am still trying to figure out why in this example the  
> function is prefixed with self.

In your example, declaring “login” as “self.login” means it’s a class  
method (similar to a static method in Java). It belongs to the User  
class, rather than to a specific instance or User, which means you  
don’t need to instantiate a new User object in order to use it.

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/3 "2006-05-05T21:03:08Z")

</div>

On May 5, 2006, at 2:51 PM, Benjamin P. wrote:

> I am working my way through some tutorials and books on Ruby and i am  
> trying to understand the various uses of the self object. For example  
> this code from Agile rails:
> 
> def self.login(name,password)
> 
> # code here
> 
> end

Here, self refers to the class  
class NamedUser

# inside here, self = NamedUser

# we are defining a method that can be called like NamedUser.login or

# NamedUser::login

def self.login(…)  
…  
end

def try\_to\_login(…)  
# inside here self will be an instance of NamedUser instead of  
# NamedUser itself  
end  
end

You should probably google singleton classes to really understand  
what’s going on

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/4 "2006-05-05T21:06:17Z")

</div>

On May 05, 2006, at 7:51 pm, Benjamin P. wrote:

> Now I understand the self.name and self password reference the calling  
> object. However, I am still trying to figure out why in this  
> example the  
> function is prefixed with self.

If by “the function” you mean “def self.login” it’s to make it a  
class method, rather than a method that applies to objects of that  
class. ie you call “User.login” and not “u = User.new; u.login”.

Ashley

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/5 "2006-05-05T21:11:08Z")

</div>

Bira wrote:

> On 5/5/06, Benjamin P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Now I understand the self.name and self password reference the calling  
> > object. However, I am still trying to figure out why in this example the  
> > function is prefixed with self.
> 
> In your example, declaring “login” as “self.login” means it’s a class  
> method (similar to a static method in Java). It belongs to the User  
> class, rather than to a specific instance or User, which means you  
> don’t need to instantiate a new User object in order to use it.

Thanks for the clarification, it really helps clear things up.

Ben

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/6 "2006-05-05T21:11:46Z")

</div>

Ashley M. wrote:

> On May 05, 2006, at 7:51 pm, Benjamin P. wrote:
> 
> > Now I understand the self.name and self password reference the calling  
> > object. However, I am still trying to figure out why in this  
> > example the  
> > function is prefixed with self.
> 
> If by “the function” you mean “def self.login” it’s to make it a  
> class method, rather than a method that applies to objects of that  
> class. ie you call “User.login” and not “u = User.new; u.login”.
> 
> Ashley

Thank you for supplying a example, It makes things even more clear then  
the other definitions provided here.

Ben

---

<div class="post-metadata">

**Author:** ![darushin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/67e7ee/32.png) [@darushin](https://www.ruby-forum.com/u/darushin)\
**Post date:** [May 5, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/question-on-self-newbie/59727/7 "2006-05-05T21:15:33Z")

</div>

On Friday 05 May 2006 12:51 pm, Benjamin P. wrote:

> def self.login(name,password)
> 
> # code here
> 
> end
> 
> def try\_to\_login  
> User.login(self.name, self.password)  
> end

> Now I understand the self.name and self password reference the calling  
> object. However, I am still trying to figure out why in this example the  
> function is prefixed with self.

Keep in mind the context that the code is executing in:

class Foo

def Foo.bar {“bar here”}

def self.qux {“qux here”}

end

Foo.bar is clear. It is declaring the method to be a class method of  
Foo.  
self.qux is doing the same thing. It is declaring qux to be a class  
method of  
whatever self is. Remember that classes are themselves objects. That  
object  
is what self contains in that instance.

Maybe this helps you see how it works?

irb(main):001:0\> class Foo  
irb(main):002:1\> puts self.object\_id  
irb(main):003:1\> end  
359445590  
=\> nil  
irb(main):004:0\> a = Foo  
=\> Foo  
irb(main):005:0\> puts a.object\_id  
359445590  
=\> nil  
irb(main):006:0\> def a.qux; 7; end  
=\> nil  
irb(main):007:0\> puts Foo.qux  
7

Kirk H.
