How to check if a number is included in a range (in one statement)?

Viewed 77987

I am using Ruby on Rails 3.0.9 and I would like to check if a number is included in a range. That is, if I have a variable number = 5 I would like to check 1 <= number <= 10 and retrieve a boolean value if the number value is included in that range.

I can do that like this:

number >= 1 && number <= 10

but I would like to do that in one statement. How can I do that?

7 Answers

If you want to check particular number exists in custom array,

As for example I want to know whether 5 is included in list=[1,4,6,10] or not

list.include? 5 => false
list.include? 6 => true
Related