Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3508

Re: Using the spaceship operator

From Brian Candler <b.candler@pobox.com>
Newsgroups comp.lang.ruby
Subject Re: Using the spaceship operator
Date 2011-04-26 05:08 -0500
Organization Service de news de lacave.net
Message-ID <81fdf3e2a9ff50645fd3988c0acd6630@ruby-forum.com> (permalink)
References <084e58ba-184f-470c-a341-68bfecc192a9@w36g2000vbi.googlegroups.com> <4aa8c4b1-e8e8-4726-9e20-42901336857d@m40g2000vbt.googlegroups.com>

Show all headers | View raw


RichardOnRails wrote in post #994401:
> While I'm still playing with delegation,  I've taken "ownership" of a
> couple of the ideas previously offered, most notably "sort_by" and:
> class Meta < Array
>   def <=>(other)
>     self[1] <=> other[1]
>   end
> end
>
> a = []
> [ [:A,12], [:B,30], [:C,4]  ]. each { |element| a <<
> Meta.new(element) }
> p a.sort

And a slightly tidier way is to use 'map' (aliased as 'collect') which 
iterates and accumulates the output array in one go:

a = [ [:A,12], [:B,30], [:C,4] ].map { |element| Meta.new(element) }
p a.sort

Regards,

Brian.

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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-20 21:13 -0700
  Re: Using the spaceship operator John Feminella <johnf@bitsbuilder.com> - 2011-04-20 23:21 -0500
  Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-21 04:41 -0500
    Re: Using the spaceship operator 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-21 12:07 -0500
  Re: Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-21 06:48 -0700
    Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-21 12:26 -0500
  Re: Using the spaceship operator RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-04-21 16:56 -0700
    Re: Using the spaceship operator Brian Candler <b.candler@pobox.com> - 2011-04-26 05:08 -0500

csiph-web