Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3302
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!talisker.lacave.net!lacave.net!not-for-mail |
|---|---|
| From | Brian Candler <b.candler@pobox.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: Using the spaceship operator |
| Date | Thu, 21 Apr 2011 04:41:29 -0500 |
| Organization | Service de news de lacave.net |
| Lines | 57 |
| Message-ID | <d2c6f554ef89586d8f63c12ccc500bc2@ruby-forum.com> (permalink) |
| References | <084e58ba-184f-470c-a341-68bfecc192a9@w36g2000vbi.googlegroups.com> |
| NNTP-Posting-Host | bristol.highgroove.com |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | talisker.lacave.net 1303379318 21533 65.111.164.187 (21 Apr 2011 09:48:38 GMT) |
| X-Complaints-To | abuse@lacave.net |
| NNTP-Posting-Date | Thu, 21 Apr 2011 09:48:38 +0000 (UTC) |
| In-Reply-To | <084e58ba-184f-470c-a341-68bfecc192a9@w36g2000vbi.googlegroups.com> |
| X-Received-From | This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway |
| X-Mail-Count | 381988 |
| X-Ml-Name | ruby-talk |
| X-Rubymirror | Yes |
| X-Ruby-Talk | <d2c6f554ef89586d8f63c12ccc500bc2@ruby-forum.com> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.ruby:3302 |
Show key headers only | View raw
RichardOnRails wrote in post #994206:
> Class MyTest < Array
> def <=>(other)
> self[1] <=> other[1]
> end
>
> @sorted_array = $date
> end
>
> How can I make this work?
Your way *can* be made to work, but it's ugly.
class MyElement < Array
def <=>(other)
self[1] <=> other[1]
end
end
a = [ MyElement.new([:A,12]),
MyElement.new([:B,30]),
MyElement.new([:C,4]),
]
a.sort!
p a
The point is that the <=> operator is applied to pairs of elements in
the array, not to the container object itself.
As a general rule I'd say: don't subclass standard Ruby classes. Use
delegation instead. So if you want a container object with Array-like
properties but special behaviour, then make it have an Array, rather
than be an Array.
class MyContainer
def initialize(a = [])
@a = a
end
def sort!
@a = @a.sort_by { |elem| elem[1] }
end
... delegate other methods as required,
... or use SimpleDelegator
end
In the longer term you'll find this a much more flexible way of
composing objects and behaviour. The only reason programming classes
tell you to subclass is because of the inflexible typing systems in
certain other languages.
Ruby has "duck typing": there is no need for your class to inherit from
Array, it just has to implement the behaviour of Array that you're
interested in.
--
Posted via http://www.ruby-forum.com/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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