[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array API

Tushar Gandhi

8/19/2008 9:07:00 AM

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?

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?

Thanks & Regards,
Tushar
--
Posted via http://www.ruby-....

6 Answers

Frederick Cheung

8/19/2008 9:44:00 AM

0


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)

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

Fred


Tushar Gandhi

8/19/2008 10:15:00 AM

0

Frederick Cheung wrote:
> 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)
>
>> 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.
>
> Fred

Hi,
Is sort_by method will cause performance problem? or Is it faster
sort?


--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

8/19/2008 12:37:00 PM

0

On Tue, Aug 19, 2008 at 12:15 PM, Tushar Gandhi
<tushar_gandhi@neovasolutions.com> wrote:
> Hi,
> Is sort_by method will cause performance problem? or Is it faster
> sort?

The theoretical performance comparison between sort and sort_by is
explained here:

http://www.ruby-doc.org/core/classes/Enumerable.ht...

Nevertheless, when talking about performance it's better to benchmark
yourself for your
specific case.

It basically all boils down to how expensive is to calculate the
comparison of two keys in the array,
versus the number of comparisons needed.

Jesus.

Robert Klemme

8/19/2008 2:10:00 PM

0

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

Frederick Cheung

8/19/2008 3:12:00 PM

0


On 19 Aug 2008, at 15:09, Robert Klemme wrote:
>>>
>>> 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:
>

Of course. it would also mean that adding methods to a module after it
had been included wouldn't work.

Fred

Frederick Cheung

8/19/2008 4:42:00 PM

0


On 19 Aug 2008, at 11:15, Tushar Gandhi wrote:
>>
>>> 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.
>>
>> Fred
>
> Hi,
> Is sort_by method will cause performance problem? or Is it faster
> sort?

It's what is often known as a schwartzian transform.
Joss Susser wrote some stuff up on sort/sort_by just a few days ago: http://blog.hasmanythrough.com/2008/8/17/sorting-...

Fred