I need to write a loop that does something like:
if i (1..10)
do thing 1
elsif i (11..20)
do thing 2
elsif i (21..30)
do thing 3
etc...
But so far have gone down the wrong paths in terms of syntax.
I need to write a loop that does something like:
if i (1..10)
do thing 1
elsif i (11..20)
do thing 2
elsif i (21..30)
do thing 3
etc...
But so far have gone down the wrong paths in terms of syntax.
As @Baldu said, use the === operator or use case/when which internally uses === :
Not a direct answer to the question, but if you want the opposite to "within":
You could use
if (1..10).cover? i then thing_1 elsif (11..20).cover? i then thing_2
and according to this benchmark in Fast Ruby is faster than
include?
if you still wanted to use ranges...
You can usually get a lot better performance with something like: