On Thu, May 28, 2009 at 10:02 AM, Brian <[email protected]> wrote:
>
> On May 28, 2:59 am, Julian Leviston <[email protected]> wrote:
>> Hi,
>>
>> >    def self.included(base)
>> >      base.send :extend, ClassMethods
>> >    end
>>
>> >    module ClassMethods
>> >      def my_method
>> >      end
>> >    end
>>
>> This is not a class method, is it? it's an instance method. a class
>> method would be
>>
>> def self.my_method
>> end
>>
>>
>
> I have all of 20 hours of Ruby experience now, so I'll probably screw
> up this explanation.  The whole include/extend thing is still very
> confusing to me, but I'm having ActiveRecord::Base call include on my
> module and attaching a callback so that I can do more.
> ActiveRecord::Base is an instance of the Class class, so when the
> callback uses extend, it is the ActiveRecord::Base instance itself
> (and ActiveRecord::Base objects) that pick up the new my_method
> instance method.  My understanding is that Ruby then attaches this to
> a metaclass for the ActiveRecord::Base instance, making it a singleton
> method.  And I think that's all class methods really are; singleton
> methods on the metadata class for an instance of the Class class.  So
> through module trickery I have created a new class method.
>
> If I have this wrong, let me know.  I don't like to write code that I
> don't understand, so the sooner I wrap my head around the actual
> mechanics of how this works, the better.

No, you have this quite right, and extending a class/module with a
'class methods' module inside another module using the included hook
is the standard way to add class methods.

-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

--~--~---------~--~----~------------~-------~--~----~
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