Forum: Ruby Question on self (Newbie)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
66e6d8d31b394eb230896caafd79beb0?d=identicon&s=25 Benjamin Pyles (darushin)
on 2006-05-05 20:51
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
439c401f95ee2fac0be4c1727dd74dea?d=identicon&s=25 Bira (Guest)
on 2006-05-05 21:02
(Received via mailing list)
On 5/5/06, Benjamin Pyles <darushin@gmail.com> 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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-05-05 21:03
(Received via mailing list)
On May 5, 2006, at 2:51 PM, Benjamin Pyles 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
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-05-05 21:06
(Received via mailing list)
On May 05, 2006, at 7:51 pm, Benjamin Pyles 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
66e6d8d31b394eb230896caafd79beb0?d=identicon&s=25 Benjamin Pyles (darushin)
on 2006-05-05 21:11
Bira wrote:
> On 5/5/06, Benjamin Pyles <darushin@gmail.com> 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
66e6d8d31b394eb230896caafd79beb0?d=identicon&s=25 Benjamin Pyles (darushin)
on 2006-05-05 21:11
Ashley Moran wrote:
> On May 05, 2006, at 7:51 pm, Benjamin Pyles 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
1b5341b64f7ce0244366eae17f06c801?d=identicon&s=25 Kirk Haines (Guest)
on 2006-05-05 21:15
(Received via mailing list)
On Friday 05 May 2006 12:51 pm, Benjamin Pyles 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 Haines
This topic is locked and can not be replied to.