146

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.

0

10 Answers 10

321
if i.between?(1, 10)
  do thing 1 
elsif i.between?(11,20)
  do thing 2 
...
5
  • 3
    This also works for Date and DateTime objects while === does not.
    – Aditya
    Commented Jun 4, 2012 at 2:23
  • i.between?(1..10) won't work (if it is ..) I suppose there must be a reason for it Commented Dec 11, 2015 at 23:46
  • between? would need two parameters it would not allow range. Commented Nov 16, 2016 at 13:12
  • 7
    is it inclusive or exclusive? Commented Aug 29, 2017 at 12:01
  • 2
    @andrewcockerham Inclusive. 3.between?(1, 3) => true Commented Aug 1, 2018 at 22:53
94

Use the === operator (or its synonym include?)

if (1..10) === i
6
  • 1
    Has the nice benefit of also working with i being something else than a number (like nil) Commented Jul 24, 2012 at 9:45
  • 4
    Wouldn't seem like a very efficient solution if the range had been significantly large.
    – rthbound
    Commented Nov 25, 2013 at 6:49
  • 6
    For future reader, the alternative way if i === (1..10) won't work
    – Anwar
    Commented May 24, 2015 at 11:13
  • @rthbound, why? (1..10000000000000000) isn't an array. (1..10000000000000000) === 5000000000000000 is just doing a "between" test under the hood Commented Jan 17, 2016 at 23:29
  • 1
    @Anwar could you explain why it doesn't work the other way?
    – Govind Rai
    Commented Feb 19, 2017 at 1:14
72

As @Baldu said, use the === operator or use case/when which internally uses === :

case i
when 1..10
  # do thing 1
when 11..20
  # do thing 2
when 21..30
  # do thing 3
etc...
1
  • of all the answers, this is also likely the most performant when you have multiple ranges.
    – xentek
    Commented Jun 9, 2013 at 3:21
42

if you still wanted to use ranges...

def foo(x)
 if (1..10).include?(x)
   puts "1 to 10"
 elsif (11..20).include?(x)
   puts "11 to 20"
 end
end
0
12

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?

1
  • This is actually a lot faster Commented Aug 29, 2017 at 9:34
10

You can usually get a lot better performance with something like:

if i >= 21
  # do thing 3
elsif i >= 11
  # do thing 2
elsif i >= 1
  # do thing 1
6

Not a direct answer to the question, but if you want the opposite to "within":

(2..5).exclude?(7)

true

1
6

If you need the fastest way to do it, use the good old comparing.

require 'benchmark'

i = 5
puts Benchmark.measure { 10000000.times {(1..10).include?(i)} }
puts Benchmark.measure { 10000000.times {i.between?(1, 10)}   }
puts Benchmark.measure { 10000000.times {1 <= i && i <= 10}   }

on my system prints:

0.959171   0.000728   0.959899 (  0.960447)
0.919812   0.001003   0.920815 (  0.921089)
0.340307   0.000000   0.340307 (  0.340358)

As you can see, double comparing is almost 3 times faster than #include? or #between? methods!

1

A more dynamic answer, which can be built in Ruby:

def select_f_from(collection, point) 
  collection.each do |cutoff, f|
    if point <= cutoff
      return f
    end
  end
  return nil
end

def foo(x)
  collection = [ [ 0, nil ],
                 [ 10, lambda { puts "doing thing 1"} ],
                 [ 20, lambda { puts "doing thing 2"} ],
                 [ 30, lambda { puts "doing thing 3"} ],
                 [ 40, nil ] ]

  f = select_f_from(collection, x)
  f.call if f
end

So, in this case, the "ranges" are really just fenced in with nils in order to catch the boundary conditions.

-2

For Strings:

(["GRACE", "WEEKLY", "DAILY5"]).include?("GRACE")

#=>true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.