Ruby between? Operator Tutorial

In this section we will learn what the between? operator is and how to use it in Ruby.

What is between? Operator in Ruby?

The between? method is a way of seeing if an object is ranked between two other objects of the same type.

For example, let’s say a user has entered an integer value into our program. Now we want to see if that value is between the value 100 and 500; this is where we can use the `between?` method.

Ruby between? Operator Syntax:

object.between? (min, max)

The method takes two arguments:

  • The first one is the min object that want to see if the target object (the one that invoked this method) is higher than the min object.
  • The second one is the max object which we want to see if the target object is less than this max object.

The result of this method is boolean value. If the target object was between the range, then the result will be true, otherwise the value false will return.

Example: using the between? operator in Ruby

puts 10.between?(5,14)



Here the value 10 is between the values 5 and 14 and so the result of calling this method is true.


Top Technologies