Array comparison returning nil

J

Julian Gall

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
 
T

ts

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

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


Guy Decoux
 
R

Robert Klemme

Julian said:
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 said:
o=Object.new
=> # said:
def o.<=>(x) nil end => nil
[o] <=> [1]
=> nil

Kind regards

robert
 
J

Julian Gall

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
 
D

dblack

Hi --

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 ([email protected])
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
 
S

Stephen Waits

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
 
L

Logan Capaldo

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

<=> 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
 
J

Julian Gall

Stephen said:
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
 
L

Logan Capaldo

Stephen said:
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

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.
 
D

dblack

Hi --

Stephen said:
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

-- 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.
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 ([email protected])
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
 
L

Logan Capaldo

--Apple-Mail-17--97628665
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


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


--Apple-Mail-17--97628665--
 
D

dblack

Hi --

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

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 ([email protected])
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
 
L

Logan Capaldo

Hi --

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

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

Good point.
 
W

William James

Julian said:
Stephen said:
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}

new_item_ids = params[:itemlist]
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.

See if this raises an error:
(original_item_ids + new_item_ids).sort
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,500
Members
48,188
Latest member
GerardRush

Latest Threads

Top