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?
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:
Maybe you could post an irb session of your own so we could see what's happening?
--Steve
···
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?
<=> 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
···
On Mar 3, 2006, at 8: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?
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].
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.
···
On Mar 3, 2006, at 1:12 PM, Julian Gall wrote:
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].
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].
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].
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:
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.
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
···
On Sat, 4 Mar 2006, Logan Capaldo wrote:
On Mar 3, 2006, at 3:33 PM, dblack@wobblini.net wrote:
=> "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.
···
On Mar 3, 2006, at 4:21 PM, dblack@wobblini.net wrote:
On Sat, 4 Mar 2006, Logan Capaldo wrote:
On Mar 3, 2006, at 3:33 PM, dblack@wobblini.net wrote: