How then? According to the docs if you implement <=> and include
Comparable you get ==. But my second assertion is failing when it
should be blowing up when trying to do nil <=> nil.
Pedro.
···
On 7/31/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> I thought date1 == date2 was the same as (date1 <=> date2> == 0 but
> apparently not.
I don't have one around. Has this changed? I'd like to make this work
now. I thought implementing <=> and including Comparable gave me ==
but something else seems to be going on.
Pedro.
···
On 7/31/06, ts <decoux@moulon.inra.fr> wrote:
> The code shouldn't even work because nil doesn't implement <=>. What
> the hell is comparable doing behind my back?
def <=>(other)
p 'At the start'
result = VALS.map{|key| self.send(key)} <=> VALS.map{|key| other.send(key)}
p 'At the end'
result
end
And the strange thing is that 'At the end' isn't printed but 'At the
start' is. How can this be?
Pedro.
···
On 7/31/06, Pedro Côrte-Real <pedro@pedrocr.net> wrote:
On 7/31/06, Robin Stocker <robin@nibor.org> wrote:
> How about something like this (untested):
>
> def <=>(other)
> VALS.map{ |key| self.send(key) } <=> VALS.map{ |key| other.send(key) }
> end
> I thought date1 == date2 was the same as (date1 <=> date2> == 0 but
> apparently not.
No, it's differently implemented.
How then? According to the docs if you implement <=> and include
Comparable you get ==. But my second assertion is failing when it
should be blowing up when trying to do nil <=> nil.
You get == but if you implement it yourself your version will shadow the one from Comparable:
>> class Foo
>> def <=>(o) 0 end
>> include Comparable
>> end
=> Foo
>> Foo.instance_method :==
=> #<UnboundMethod: Foo(Comparable)#==>
Now we add it to Foo:
>> class Foo
>> def ==(o) false end
>> end
=> nil
>> Foo.instance_method :==
=> #<UnboundMethod: Foo#==>
>> Foo.ancestors
=> [Foo, Comparable, Object, Kernel]
Kind regards
robert
···
On 7/31/06, Robert Klemme <shortcutter@googlemail.com> wrote: