Determining if a variable is within range?

后端 未结 9 942
自闭症患者
自闭症患者 2020-12-12 12:02

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...
9条回答
  •  自闭症患者
    2020-12-12 13:09

    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
    

提交回复
热议问题