From: dblack@... Date: 2006-03-04T05:33:54+09:00 Subject: Re: Array comparison returning nil Hi -- On Sat, 4 Mar 2006, Logan Capaldo wrote: > > 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-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 (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