You could use a case
expression:
case rand
when 0..9
print 'yes'
else
print 'no'
end
It also allows you to provide multiple ranges or numbers to compare against. Here's a contrived example:
case rand
when 0..2, 3, 4..9
print 'yes'
else
print 'no'
end
Under the hood, case
uses ===
to compare the given objects, i.e. the above is (almost) equivalent to:
if 0..2 === rand || 3 === rand || 4..9 === rand
print 'yes'
else
print 'no'
end
(note that the when
objects become the receiver and rand
becomes the argument)