On Wed, 19 Oct 2005, Sean O'Halpin wrote:

> On 10/19/05, Ara.T.Howard <Ara.T.Howard / noaa.gov> wrote:
>> this avoids calling to_i, to_f, or whatever multiple times on the same object,
>> which will occur if you use either a spacship (<=>) operator or sort_by
>> approach.
>
> I thought sort_by was a packaged form of the Schwartzian transform, i.e.
>
> class A
>  def to_i
>    puts "in A.to_i"
>    3
>  end
> end
>
> p [1, 2, 3, 4, 5, A.new].sort_by {|x| x.to_i }
> # is equivalent to
> p [1, 2, 3, 4, 5, A.new].map{|x| [x.to_i, x]}.sort{|y, z| y[0] <=>
> z[0]}.map{|x| x[1]}
>
> __END__
> in A.to_i
> [1, 2, 3, #<A:0x2870f28>, 4, 5]
> in A.to_i
> [1, 2, 3, #<A:0x2870cd0>, 4, 5]

you are quite right sean - i guess that only applies to the op's original

   a.to_i <=> b.to_i

where you could end up doing that more than once.

regards.

-a
-- 
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| anything that contradicts experience and logic should be abandoned.
| -- h.h. the 14th dalai lama
===============================================================================