Range operator in Ruby

2020-12-16 hit count image

Let's see what Range operator is and how to use it.


In this blog post, we’ll see what Range operator is and how to use it. When you use the Range operator, you should set ordered integers, alphabets in accending order.

The Range operator is Ruby’s unique grammar, and it is often used.

Type of Range operator

Range operator has two types, .. and .... The difference of .. and ... is that ... doesn’t include the last element.

How to use the Range operator

You can use the Range operator like below.

# 1, 2, 3, 4
# 1, 2, 3
# a, b, c
# a, b


As you see above, Ruby has a unique grammar of the Range operator. The Range operator is normally used in the loop.

(1...4).each do |i|
  puts i
# 1
# 2
# 3

case 79
when 1..50   then   print "low\n"
when 51..75  then   print "medium\n"
when 76..100 then   print "high\n"
# high

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.