[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: self.included question

Robert Dober

5/16/2007 8:19:00 PM

On 5/16/07, Dave Hoefler <dhoefler@gmail.com> wrote:
> Hi there,
>
> I see the following code chunk in a lot of Rails plugins:
>
> def self.included(base)
> base.extend(ClassMethods)
> end
>
> module ClassMethods
> def method_one
> end
>
> def method_two
> end
> end
>
>
> I'm most curious about "self.included". I checked out the docs on 'included'
> (http://www.ruby-doc.org/core/classes/Module.ht...).
This is the doc about #include not #included!!
>They pointed to
> 'Module.append_features' (
> http://www.ruby-doc.org/core/classes/Module.ht...). Even after
> reading the docs, I'm still a little lost. Could someone break it down a
> little more than the docs? ...or point me at some other resource. I'd like
> to know "why" this is done.
>
> Thanks,
> Dave
>

The included hook is run whenever a module is included.
The parameter base passed to the hook is referring to the class into
which the module is included.
Basically
def self.included(base)
base.extend(ClassMethods)
end
means:
and if you include me you also extend your class to all methods
defined in ClassMethods.

This means that instead of writing

class X
include Y
extend ClassMethods
end

you write
class X
include Y # ==> the hook does X.extend(ClassMethods)
end

Although I do not know Rails it might be a reasonable guess that the
functionality of ClassMethods is closely coupled with the
functionality of the inserted module - and that methods of the module
need class methods from ClassMethods.

HTH
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

2 Answers

WoNáDo

5/16/2007 8:36:00 PM

0

Robert Dober schrieb:
>> I'm most curious about "self.included". I checked out the docs on
>> 'included'
>> (http://www.ruby-doc.org/core/classes/Module.ht...).
> This is the doc about #include not #included!!

You are looking for the wrong Method - see documentation about "Object#extend".
The usages are visible in the example:

module ClassMethods
def method_one
puts "in method_one"
end

def method_two
puts "in method_two"
end
end

def incl1(x)
x.extend(ClassMethods)
end

module Mymod
def self.incl2(x)
x.extend(ClassMethods)
end
end

incl1(String)
Mymod.incl2(Array)


String.method_one # => in method_one
String.method_two # => in method_two

Array.method_one # => in method_one
Array.method_one # => in method_one

Wolfgang Nádasi-Donner

Robert Dober

5/16/2007 8:43:00 PM

0

On 5/16/07, Wolfgang Nádasi-Donner <wonado@donnerweb.de> wrote:
> Robert Dober schrieb:
No it was not me, it was OP!
> >> I'm most curious about "self.included". I checked out the docs on
> >> 'included'
> >> (http://www.ruby-doc.org/core/classes/Module.ht...).
> > This is the doc about #include not #included!!
>
<snip>
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw