Robert Klemme
8/19/2008 2:10:00 PM
2008/8/19 Frederick Cheung <frederick.cheung@gmail.com>:
>
> On 19 Aug 2008, at 10:06, Tushar Gandhi wrote:
>
>> Hi,
>> I am going through the ruby Array API. In the API , there is staetment
>>
>> Included Modules
>> Enumerable
>>
>> Is this means that we are inheriting the enumerable class to array?
>>
> Not inheriting. That means the Enumerable module is mixed in to Array (and
> similarly to Hash and other collections)
Actually, it is a form of multiple inheritance.
irb(main):001:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel]
>> Also there is sort_by method for the enumerable object, but I am able to
>> use this method for array of object.
>>
>> I have this type of array of Object:-
>> [#<UserDemo:0x89a5bd4 @attributes={"email3"=>nil, "lastname"=>"aikhe",
>> "firstname"=>"mita", "id"=>"1"}>,#<UserDemo:0x89a5bd4
>> @attributes={"email3"=>nil, "lastname"=>"aikhe", "firstname"=>"na",
>> "id"=>"2"}>]
>>
>> How it is possible that we are able to use enumerable sort_by method for
>> an array of object?
>>
> Because that's what including a module does: it adds the methods defined in
> the module to the class.
This is only true in a very general sense, i.e. if you want to express
with this that you can invoke methods defined in a module on an
instance of a class that directly or indirectly (!) inherits the
module.
irb(main):002:0> Array.instance_method :sort_by
=> #<UnboundMethod: Array(Enumerable)#sort_by>
irb(main):003:0> Array.instance_method :size
=> #<UnboundMethod: Array#size>
Note the difference between #sort_by (defined in Enumerable) and #size
(defined in Array).
Rather the module is inserted into the lookup path (see output of
#ancestors above). Otherwise methods from different modules could not
invoke each other via super but instead one would overwrite the other:
irb(main):004:0> class Base; def x; puts "base"; end; end
=> nil
irb(main):005:0> module A; def x;puts "mod a"; super; end; end
=> nil
irb(main):006:0> module B; def x;puts "mod b"; super; end; end
=> nil
irb(main):007:0> class D1 < Base; include A; include B; end
=> D1
irb(main):008:0> class D2 < Base; include B; include A; end
=> D2
irb(main):009:0> D1.new.x
mod b
mod a
base
=> nil
irb(main):010:0> D2.new.x
mod a
mod b
base
=> nil
irb(main):011:0> D1.ancestors
=> [D1, B, A, Base, Object, Kernel]
irb(main):012:0> D2.ancestors
=> [D2, A, B, Base, Object, Kernel]
Kind regards
robert
--
use.inject do |as, often| as.you_can - without end