94

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?

2
  • Since there's nothing wrong with your cited statement it begs the question: why do want/need to further "shorten" it ?
    – DarkDust
    Commented Jul 30, 2011 at 8:07
  • 1
    @DarkDust - Less code as well as possible!
    – Backo
    Commented Jul 30, 2011 at 10:08

7 Answers 7

179

(1..10).include?(number) is the trick.

Btw: If you want to validate a number using ActiveModel::Validations, you can even do:

validates_inclusion_of :number, :in => 1..10

read here about validates_inclusion_of

or the Rails 3+ way:

validates :number, :inclusion => 1..10
2
  • What if I want also to allow nil? I'm getting an error when it's nil. Commented Jan 7, 2014 at 14:45
  • 6
    Rails has a option allow_nil. Just add it like so: , allow_nil: true
    – Mario Uher
    Commented Jan 7, 2014 at 14:48
78

Enumerable#include?:

(1..10).include? n

Range#cover?:

(1..10).cover? n

Comparable#between?:

n.between? 1, 10

Numericality Validator:

validates :n, numericality: {only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 10}

Inclusion Validator:

validates :n, inclusion: 1..10
4
  • 1
    I like this method better, because the validation text makes more sense.
    – Dan
    Commented Jun 18, 2013 at 15:19
  • I like it better because I was already validation numericality. Makes it a one-line :)
    – BradGreens
    Commented Sep 12, 2013 at 1:13
  • @BradGreens that's one long line.
    – wchargin
    Commented Dec 27, 2013 at 6:06
  • 1
    I want to note that u can use proc if u need some specific logic or access to associations. For example validates :amount, numericality: { less_than_or_equal_to: ->(m) { m.user.balance }, greater_than_or_equal_to: 0 } Commented Mar 22, 2016 at 12:02
17

If it's not part of a validation process you can use #between? :

2.between?(1, 4)
=> true
9

For accurate error messages on a form submit , try these

validates_numericality_of :tax_rate, greater_than_or_equal_to: 0, less_than_or_equal_to: 100, message: 'must be between 0 & 100'
4

Rails 4

if you want it through ActiveModel::Validations you can use

validates_inclusion_of :number, :in => start_number..end_number

or the Rails 3 syntax

validates :number, :inclusion => start_number..end_number

But The simplest way i find is

number.between? start_number, end_number

1
  • Perhaps include some explanation.
    – GMchris
    Commented May 26, 2016 at 11:33
2

In Ruby 1.9 the most direct translation seems to be Range#cover?:

Returns true if obj is between beg and end, i.e beg <= obj <= end (or end exclusive when exclude_end? is true).

In case you wonder how that's different from Range#include?, it's that the latter iterates over all elements of the range if it's a non-numeric range. See this blog post for a more detailed explanation.

1

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

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.