[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Array comparison returning nil

Julian Gall

3/3/2006 1:38:00 PM

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

--
Posted via http://www.ruby-....


14 Answers

ts

3/3/2006 1:53:00 PM

0

>>>>> "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


Robert Klemme

3/3/2006 1:53:00 PM

0

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

Julian Gall

3/3/2006 1:57:00 PM

0

But http://www.rubycentral.com/ref/ref_c_array.html...

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

I sometimes get 0 and sometimes nil.

Julian


--
Posted via http://www.ruby-....


Julian Gall

3/3/2006 2:41:00 PM

0

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

--
Posted via http://www.ruby-....


dblack

3/3/2006 3:20:00 PM

0

Stephen Waits

3/3/2006 5:14:00 PM

0


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


Logan Capaldo

3/3/2006 5:44:00 PM

0


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?
>
> Thanks,
>
> Julian
>
> --
> Posted via http://www.ruby-....
>

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




Julian Gall

3/3/2006 6:13:00 PM

0

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

--
Posted via http://www.ruby-....


Logan Capaldo

3/3/2006 8:15:00 PM

0


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

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.





dblack

3/3/2006 8:34:00 PM

0