[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Flow of execution

Mark Woodward

1/29/2007 9:45:00 AM

Hi all,

Could someone explain the 'flow' of how the eg below is processed?
My limited understanding is that the left hand side is the receiver and
the right hand side the method being passed to the receiver?

What I don't understand is where the block comes into the equation for
something like the following:

values = anagrams.values.sort do |a, b|
b.length <=> a.length
end


The block changes how the sort method sorts? ie it sorts by length rather
than alphabetically? So it overrides the default meaning of <=>??

We get the values of the anagram hash by the 'values' method?
We then do:

..sort do |a, b| b.length <=> a.length end

ie this whole line acts on the values array??
not a 2 step process of 1. the sort method then 2. the block applied in
some way?

To illustrate what I'm failing dismally to explain here:
Using () for grouping, is it processed as A or B?

A
values = anagrams.values.(sort do |a, b| b.length <=> a.length)
end

B
values = anagrams.values.(sort) (do |a, b| b.length <=> a.length)
end


thanks,


--
Mark
2 Answers

Alex Young

1/29/2007 10:10:00 AM

0

Mark Woodward wrote:
> Hi all,
>
> Could someone explain the 'flow' of how the eg below is processed?
> My limited understanding is that the left hand side is the receiver and
> the right hand side the method being passed to the receiver?
>
> What I don't understand is where the block comes into the equation for
> something like the following:
>
> values = anagrams.values.sort do |a, b|
> b.length <=> a.length
> end
>
>
> The block changes how the sort method sorts? ie it sorts by length rather
> than alphabetically? So it overrides the default meaning of <=>??
>
> We get the values of the anagram hash by the 'values' method?
> We then do:
>
> .sort do |a, b| b.length <=> a.length end
>
> ie this whole line acts on the values array??
> not a 2 step process of 1. the sort method then 2. the block applied in
> some way?
>
> To illustrate what I'm failing dismally to explain here:
> Using () for grouping, is it processed as A or B?
>
> A
> values = anagrams.values.(sort do |a, b| b.length <=> a.length)
> end
This, almost. It's more like:

values = anagrams.values.sort(do |a,b| b.length <=> a.length end)

The block essentially acts as a parameter to the sort method. The sort
method uses the result of passing each pair to the block to perform the
actual sorting.

--
Alex

Mark Woodward

1/29/2007 10:24:00 AM

0

Hi Alex,

On Mon, 29 Jan 2007 19:09:35 +0900, Alex Young wrote:

> Mark Woodward wrote:
>> Hi all,
>>
>> Could someone explain the 'flow' of how the eg below is processed?
>> My limited understanding is that the left hand side is the receiver and
>> the right hand side the method being passed to the receiver?
>>
>> What I don't understand is where the block comes into the equation for
>> something like the following:
>>
>> values = anagrams.values.sort do |a, b|
>> b.length <=> a.length
>> end
>>
>>
>> The block changes how the sort method sorts? ie it sorts by length rather
>> than alphabetically? So it overrides the default meaning of <=>??
>>
>> We get the values of the anagram hash by the 'values' method?
>> We then do:
>>
>> .sort do |a, b| b.length <=> a.length end
>>
>> ie this whole line acts on the values array??
>> not a 2 step process of 1. the sort method then 2. the block applied in
>> some way?
>>
>> To illustrate what I'm failing dismally to explain here:
>> Using () for grouping, is it processed as A or B?
>>
>> A
>> values = anagrams.values.(sort do |a, b| b.length <=> a.length)
>> end
> This, almost. It's more like:
>
> values = anagrams.values.sort(do |a,b| b.length <=> a.length end)
>
> The block essentially acts as a parameter to the sort method. The sort
> method uses the result of passing each pair to the block to perform the
> actual sorting.


excellent, thanks.

--
Mark