Question on self (Newbie)

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

On 5/5/06, Benjamin P. [email protected] 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.

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

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

Bira wrote:

On 5/5/06, Benjamin P. [email protected] 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

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

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.