[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

comparison of Array with Array failed

c-note

5/20/2008 12:22:00 PM

Hi all,

i got this error when the field serial is empty.


Showing app/views/find/index.rhtml where line #44 raised:

comparison of Array with Array failed

Extracted source (around line #44):

41: <% equipements = Equipement.find(:all,
:conditions => [ 'id != "-1" ' ])
42: @tab = [["--- Choose ---", -1]]
43: @liste = @equipements.collect {|elt| [ elt.serial, elt.id ]}
44: @thetab = @tab + @liste.sort
45: %>
46: <%=
47: select("profile2", "modele_om", @thetab) %>

Can you help me ?

Thanks
1 Answer

Kyle

5/20/2008 7:58:00 PM

0

I don't know if this is the root or not. Array#sort uses the <=>
operator:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_array.html#...

This operator should return -1, 0, or 1. According to my very brief
test in irb, an empty serial might be fine while a nil serial will
throw an error.

irb(main):001:0> a = ["serial", "1"]
=> ["serial", "1"]
irb(main):002:0> b = ["", "2"]
=> ["", "2"]
irb(main):003:0> a <=> b
=> 1
irb(main):004:0> b = [nil, "2"]
=> [nil, "2"]
irb(main):005:0> a <=> b
=> nil

My guess is that when #sort gets nil instead of -1, 0, or 1, it throws
the error. Of course, I haven't looked deeply into the source, so I
could be wrong. Again, irb suggests this:

irb(main):006:0> arr = [a, b]
=> [["serial", "1"], [nil, "2"]]
irb(main):007:0> arr.sort
ArgumentError: comparison of Array with Array failed
from (irb):7:in `sort'
from (irb):7
from :0

Perhaps you could replace "elt.serial" with "(elt.serial || '')". If
that doesn't work - or if my ideas are horrible - somebody else will
help you, I'm sure.

-Kyle

On May 20, 7:25 am, c-note <c-n...@hotmail.fr> wrote:
> Hi all,
>
> i got this error when the field serial is empty.
>
> Showing app/views/find/index.rhtml where line #44 raised:
>
> comparison of Array with Array failed
>
> Extracted source (around line #44):
>
> 41: <% equipements = Equipement.find(:all,
> :conditions => [ 'id != "-1" ' ])
> 42: @tab = [["--- Choose ---", -1]]
> 43: @liste = @equipements.collect {|elt| [ elt.serial, elt.id ]}
> 44: @thetab = @tab + @liste.sort
> 45: %>
> 46: <%=
> 47: select("profile2", "modele_om", @thetab) %>
>
> Can you help me ?
>
> Thanks