Forum: Ruby Array comparison returning nil

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-03 14:37
I am comparing two arrays with:

original_item_ids.sort <=> new_item_ids.sort

However, this returns nil, which seems to be against the documentation
which says the result will be -1, 0 or 1. Both arrays contain the same
values, alhough in a different order, hence the sort.

Does anyone know of any circumstance that would cause this?

Thanks,

Julian
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-03-03 14:53
(Received via mailing list)
>>>>> "J" == Julian Gall <julian.gall@gmail.com> writes:

J> Does anyone know of any circumstance that would cause this?

moulon% ruby -e 'p ["a" , 2] <=> [3, 4]'
nil
moulon%


Guy Decoux
5befe95e6648daec3dd5728cd36602d0?d=identicon&s=25 Robert Klemme (Guest)
on 2006-03-03 14:56
(Received via mailing list)
Julian Gall wrote:
> I am comparing two arrays with:
>
> original_item_ids.sort <=> new_item_ids.sort
>
> However, this returns nil, which seems to be against the documentation
> which says the result will be -1, 0 or 1. Both arrays contain the same
> values, alhough in a different order, hence the sort.
>
> Does anyone know of any circumstance that would cause this?

Improper implemented <=> method on one of the array members:

>> o=Object.new
=> #<Object:0x4a7c118>
>> def o.<=>(x) nil end
=> nil
>> [o] <=> [1]
=> nil

Kind regards

    robert
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-03 14:57
But http://www.rubycentral.com/ref/ref_c_array.html#_lt_eq_gt

"Returns an integer -1, 0, or +1 ..."

I sometimes get 0 and sometimes nil.

Julian
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-03 15:41
It seems <=> is not much use if it can't be relied on to do what it
says.

I now need to compare my arrays with:

original_item_ids.sort.join('-') == new_item_ids.sort.join('-')

Which is messy.

Julian
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-03 16:21
(Received via mailing list)
Hi --

On Fri, 3 Mar 2006, Julian Gall wrote:

> It seems <=> is not much use if it can't be relied on to do what it
> says.
>
> I now need to compare my arrays with:
>
> original_item_ids.sort.join('-') == new_item_ids.sort.join('-')
>
> Which is messy.

Can you show an example of two arrays with the same elements that give
you nil when sorted and compared with <=> ?


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
280b41a88665fd8c699e83a9a25ef949?d=identicon&s=25 Stephen Waits (Guest)
on 2006-03-03 18:16
(Received via mailing list)
On Mar 3, 2006, at 5:37 AM, Julian Gall wrote:

> I am comparing two arrays with:
>
> original_item_ids.sort <=> new_item_ids.sort
>
> However, this returns nil, which seems to be against the documentation
> which says the result will be -1, 0 or 1. Both arrays contain the same
> values, alhough in a different order, hence the sort.
>
> Does anyone know of any circumstance that would cause this?

It works for me...

% irb
irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> b = [3,1,2]
=> [3, 1, 2]
irb(main):003:0> a.sort <=> b.sort
=> 0

Maybe you could post an irb session of your own so we could see
what's happening?

--Steve
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-03 18:47
(Received via mailing list)
On Mar 3, 2006, at 8:37 AM, Julian Gall wrote:

> Thanks,
>
> Julian
>
> --
> Posted via http://www.ruby-forum.com/.
>

<=> will return nil when a comparision makes no sense

eg:

irb(main):013:0> a = "a"
=> "a"
irb(main):014:0> b = 2
=> 2
irb(main):015:0> a <=> b
=> nil
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-03 19:12
Stephen Waits wrote:
> Maybe you could post an irb session of your own so we could see
> what's happening?

Not really possible. I am writing a Rails application with a sortable
list on a web page. An XML_Http_request serialises the ids of the list
items and POSTs them to my action controller. By the time I see this
data it is an array of identifiers (numbers) in params[:itemlist].

I extract this to a local array with:

    new_item_ids = []
    params[:itemlist].each {|item| new_item_ids << item}

I want to compare this with another local array of my original
(unsorted) identifiers. i.e.

    original_item_ids.sort <=> new_item_ids.sort

This doesn't work, although this does:

    original_item_ids.sort.join('-') == new_item_ids.sort.join('-')

It is odd that join works to produce the expected string where <=>
thinks it has a problem.

Julian
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-03 21:18
(Received via mailing list)
On Mar 3, 2006, at 1:12 PM, Julian Gall wrote:

>
>     original_item_ids.sort.join('-') == new_item_ids.sort.join('-')
>
> It is odd that join works to produce the expected string where <=>
> thinks it has a problem.
>
> Julian
>
> --
> Posted via http://www.ruby-forum.com/.
>

Based on join working this is my theory. One  of the two arrays have
nil's in it. nil.to_s is "" but nil <=> anything save  nil is nil and
will cause the comparision to fail.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-03 21:36
(Received via mailing list)
Hi --

On Sat, 4 Mar 2006, Logan Capaldo wrote:

>> data it is an array of identifiers (numbers) in params[:itemlist].
>>
>>
>
> Based on join working this is my theory. One  of the two arrays have nil's in
> it. nil.to_s is "" but nil <=> anything save  nil is nil and will cause the
> comparision to fail.

However:

>> [1,2,nil,3].join("-")
=> "1-2--3"

so you'd get those extra -'s in one but not in the other if one but
not the other had nils.

I wonder whether the params[:itemlist] is producing strings while the
original array is integers.  That would explain join working but <=>
giving up:

irb(main):009:0> ["1","2"] <=> [1,2]
=> nil
irb(main):010:0> ["1","2"].join("-") == [1,2].join("-")
=> true


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-03 21:39
(Received via mailing list)
On Mar 3, 2006, at 3:33 PM, dblack@wobblini.net wrote:

> However:
>
>>> [1,2,nil,3].join("-")
> => "1-2--3"
>
> so you'd get those extra -'s in one but not in the other if one but
> not the other had nils.

I doubt he's displaying the result of the join when doing the
comparison, it would still compare whether or not there are double
dashes
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-03 22:24
(Received via mailing list)
Hi --

On Sat, 4 Mar 2006, Logan Capaldo wrote:

>
> I doubt he's displaying the result of the join when doing the comparison, it
> would still compare whether or not there are double dashes

It's not a display thing; it's a <=> or == thing.  If the two arrays
have unequal numbers of nils, and you join them with "-", the results
will not be equal.  Actually, I realize now that since he's sorting
them, he'll never get that far anyway, because this:

   [1,2,3,nil].sort

won't work at all; nor will ["1", "2", "3", nil].sort.

I'll keep my money on the theory that one array contains integers and
one contains string representations of the same integers :-)


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-03 22:43
(Received via mailing list)
On Mar 3, 2006, at 4:21 PM, dblack@wobblini.net wrote:

>>> so you'd get those extra -'s in one but not in the other if one but
>
>   [1,2,3,nil].sort
>
> won't work at all; nor will ["1", "2", "3", nil].sort.
>
> I'll keep my money on the theory that one array contains integers and
> one contains string representations of the same integers :-)
>
>
> David
>

Good point.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2006-03-03 23:58
(Received via mailing list)
Julian Gall wrote:
>
>     new_item_ids = []
>     params[:itemlist].each {|item| new_item_ids << item}
>

new_item_ids = params[:itemlist]

> thinks it has a problem.
See if this raises an error:
(original_item_ids + new_item_ids).sort
7284167ee91af66fcd739a42e3df3804?d=identicon&s=25 Julian Gall (juliangall)
on 2006-03-04 22:29
Thanks very much to Logan, David and William. David hit on the answer by
pointing out that one array has integers and the other has strings. When
I load my original id array with the values .to_s, the comparison works
as expected. Hooray! I should have realised that anything returned from
a web page would be a string.

Anyway, thanks very much, guys, for taking the time to follow this
through. I really appreciate it.

Julian
This topic is locked and can not be replied to.