On Sat, 19 Jul 2003 15:58:51 +0900, Kurt M. Dresner wrote:

> When I learned python I was overjoyed that I could evaluate 1 < 2 < 3
> and get "true".  I just realized that you can't do that in Ruby.  Is
> there a reason why?  Is it good?  I know I can use "between", but
> still...


You can do this:

if [1, 2, 3].ordered?
    puts "ok"
end


It will require that you extend the Array class yourself, like this:

> expand -t4 b.rb
class Array
    def ordered?
        return true if self.empty?
        a = self.first
        self[1..-1].each { |b|
            return false if a > b
            a = b
        }
        true
    end
end

p [].ordered?             #=> true
p [2].ordered?            #=> true
p [-1, 2, 3, 5].ordered?  #=> true
p [17, 13, 11].ordered?   #=> false
p [1, -1, 1, 0].ordered?  #=> false
>

The output is:

> ruby b.rb
true
true
true
false
false
>


--
Simon Strandgaard