[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

NilClass and Comparable

RubyTalk@gmail.com

5/8/2009 11:00:00 PM

>> nil==0
=> false
>> class NilClass
>> include Comparable
>> def <=>(other)
>> 0
>> end
>> end
=> nil
>> [1,nil,2].sort
ArgumentError: comparison of Fixnum with nil failed
from (irb):11:in `sort'
from (irb):11
>> p nil > 2
false
=> nil
>> p nil > -1
false
=> nil
>> p nil == 0
true
=> nil



What I am missing here?

4 Answers

Adam Gardner

5/8/2009 11:23:00 PM

0

RubyTalk@gmail.com wrote:
>>> nil==0
> => false
>>> class NilClass
>>> include Comparable
>>> def <=>(other)
>>> 0
>>> end
>>> end
> => nil
>>> [1,nil,2].sort
> ArgumentError: comparison of Fixnum with nil failed
> from (irb):11:in `sort'
> from (irb):11
>>> p nil > 2
> false
> => nil
>>> p nil > -1
> false
> => nil
>>> p nil == 0
> true
> => nil
>
>
>
> What I am missing here?

Probably this:

irb(main):003:0> class NilClass
irb(main):004:1> def <=>(other)
irb(main):005:2> 0
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> nil <=> 1
=> 0
irb(main):009:0> 1 <=> nil
=> nil
irb(main):010:0>
--
Posted via http://www.ruby-....

RubyTalk@gmail.com

5/8/2009 11:35:00 PM

0

Yes! Thats exactly what I was missing. Why would someone write <=3D> for n=
il?

On Fri, May 8, 2009 at 4:23 PM, Adam Gardner <adam.oddfellow@gmail.com> wro=
te:
> RubyTalk@gmail.com wrote:
>>>> nil=3D=3D0
>> =3D> false
>>>> class NilClass
>>>> include Comparable
>>>> def <=3D>(other)
>>>> =A00
>>>> end
>>>> end
>> =3D> nil
>>>> [1,nil,2].sort
>> ArgumentError: comparison of Fixnum with nil failed
>> =A0 from (irb):11:in `sort'
>> =A0 from (irb):11
>>>> p nil > 2
>> false
>> =3D> nil
>>>> p nil > -1
>> false
>> =3D> nil
>>>> p nil =3D=3D 0
>> true
>> =3D> nil
>>
>>
>>
>> What I am missing here?
>
> Probably this:
>
> irb(main):003:0> class NilClass
> irb(main):004:1> def <=3D>(other)
> irb(main):005:2> 0
> irb(main):006:2> end
> irb(main):007:1> end
> =3D> nil
> irb(main):008:0> nil <=3D> 1
> =3D> 0
> irb(main):009:0> 1 <=3D> nil
> =3D> nil
> irb(main):010:0>
> --
> Posted via http://www.ruby-....
>
>

Robert Klemme

5/9/2009 4:51:00 PM

0

On 09.05.2009 01:35, RubyTalk@gmail.com wrote:
> Yes! Thats exactly what I was missing. Why would someone write <=> for nil?

There is probably no good reason because nothing doesn't compare well to
anything - and so the method is not defined. :-)

Kind regards

robert

Brian Candler

5/11/2009 1:38:00 PM

0

Robert Klemme wrote:
> On 09.05.2009 01:35, RubyTalk@gmail.com wrote:
>> Yes! Thats exactly what I was missing. Why would someone write <=> for nil?
>
> There is probably no good reason because nothing doesn't compare well to
> anything - and so the method is not defined. :-)

It can be useful to have an ordering defined for mixed collections. For
example, CouchDB defines a full ordering across null, numbers, strings,
arrays and objects (hashes):
http://wiki.apache.org/couchdb/View...

In Ruby, the solution depends on what exactly you're trying to do. Maybe
sort_by with a default value is good enough for what you need.

arr.sort_by { |e| e || -1 }

You can also use arr.sort { |a,b| ... } and then the block defines the
'<=>' logic for comparing two objects.

Note that it would be dangerous to define nil <=> any and any <=> nil to
be 0, because that would mean (any number) == nil == (any other number).
For example,

[2, nil, 1]

could be treated as an already-sorted array!

Instead, you might want to define nil <=> x as -1 and x <=> nil as +1
(where x is any non-nil value). You can do that in your sort block.

Regards,

Brian.

P.S. Also be aware that ruby's sort is implemented using quicksort
(IIRC), which is not stable for equal keys.
--
Posted via http://www.ruby-....