Our Daily Method #2: Numeric#in
Geplaatst door Michiel de Mare wo, 06 feb 2008 06:00:00 GMT
Most web-applications do not appear to have a lot to do with random numbers, online poker sites excepted (I hope), but they are surprisingly useful nevertheless. That’s why I’m introducing a new notation to express chance.
# this certainly works
redirect_to home_url if rand < 0.2222
# this is better
redirect_to home_url if rand(9) < 2
# but not as beautiful as this
redirect_to home_url if 2.in 9
# the code
class Numeric
def in(other)
rand(other) < self
end
end