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