I have a simple Audio-CD database (using CSV format). I was writing a
piece of Ruby code to list what's in the DB. And actually I wanted to
display the entries sorted by Artist and by Album.


Originally, I wrote that:

db.sort { |a,b| a[1]['Artist'] <=> b[1]['Artist'] ||
                a[1]['Album'] <=> b[1]['Album'] }.each { |t|
        ...
}


This does not work, for the simple reason that in Ruby `0' is true. Ok, I
can imagine that 0 (and actually "", [], {}) could be true because Ruby
wants me to give a real Boolean rather than a C-like broken truth value...

But now, it doesn't throw me an exception... probably because there is
more than "false" to be false, actually "nil" is also false. Probably
because not having the ability to use the truth value of #[] on an Array
and a Hash to detect a missing entry would be a pain !?


So now, here's more or less what I wrote:

def myboolor(a, b)
    if a != 0 then a else b end
end

db.sort { |a,b| myboolor(a[1]['Artist'] <=> b[1]['Artist'], a[1]['Album'] <=> b[1]['Album']) }.each { |t|
        ...
}


And if I don't want to use a subfunction, I need to use a temp variable
inside the sort {} method...

I think that this lack of consistency (you may take the truth value for
something which can be "nil", not for something which can be "0" or an
empty string) is likely to shoot the "principle of least surprise" that
Matz likes ;p. And actually it shot me; and I find the upper solution not
very pretty...



-- 
Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/