Python vs Ruby: y greater than x and less than z?

Is there a less accurate way to compare three integer values ​​in Ruby?

For example, in Python, the following return True:

x = 2
y = 3
z = 4
x < y < z

With the same variable bindings in Ruby, the following returns both values:

x < y && y < z
x.send(:<, y) && y.send(:<, z)

but this:

x < y < z

returns NoMethodError:

NoMethodError: undefined method `<' for true:TrueClass

I assume that this is due to the fact that the first comparison x < yis evaluated as true, and the error arises from the received TrueClass.instance < z? Is there any way in Ruby to compare three integer values ​​without using & &?

Thank.

+4
source share
1 answer

You can write

(x+1...z).cover? y

or (my preference)

(x+1..z-1).cover? y

Since x, yand zare numerical, this is the same as

(x+1..z-1).include? y

. # # ?.

+2

Source: https://habr.com/ru/post/1656597/


All Articles