From: Dave Thomas Date: 2001-08-24T07:04:01+09:00 Subject: [ruby-talk:20213] Re: In Ruby 0 is true but nil is false.. or how to shoot yourself?.. Guillaume Cottenceau writes: > 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| > ... > } Try 'nonzero?' dave[~ 13:17:19] ri nonzero ------------------------------------------------------- Numeric#nonzero? num.nonzero? -> num or nil ------------------------------------------------------------------------ Returns num if num is not zero, nil otherwise. This behavior is useful when chaining comparisons: a = %w( z Bb bB bb BB a aA Aa AA A ) b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b } b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"] Regards Dave