Hi --

On Mon, 13 Jul 2009, Buzz Hill wrote:

>
> JangoSteve wrote:
>> In other words, you just use def get_payment_types if
>> get_payment_types is to be performed on a specific instance of that
>> class, and def self.get_payment_types if it is just a generic function
>> for that class...
>
> I guess what I am confused about is this:
>
> Dave Thomas talks about self as a special variable used by Ruby to
> maintain a reference to the context of where the interpreter is
> operating and any given point, so it knows where to find a particular
> method. So why does Ruby not seem to know the context of
> "get_payment_types" without the self. Here is a slight variation on the
> previous code:

I think you're confusing defining a method with calling a method. self
always serves as the default receiver for messages -- meaning that
this:

   my_method(a,b,c)

is interpreted as this:

   self.my_method(a,b,c)

When you're defining a method, if you designate a specific object for
the method, like this:

   def my_object.some_method(a,b,c)
     # ...
   end

then the method will be a singleton method on that object -- meaning
that the method is available only to that object.

So when you do this:

   class Something
     def self.greet
       puts "Hello from #{self}!"
     end
   end

you're defining a method on the object self -- which happens to be, at
that point in execution, the class object Something.

There's a bit more to it, but am I on the right track, in terms of
what you're finding confusing?


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to