×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to demonstrate the range (..) operator

Last Updated : December 15, 2025

Problem Solution

In this program, we will create the range of integer and string elements using the range (..) operator. The range (..) operator returns the range of elements including the last term.

Program/Source Code

The source code to demonstrate the range (..) operator is given below. The given program is compiled and executed successfully.

# Ruby program to demonstrate the 
# range (..) operator

num_range = (5..8).to_a();
str_range = ('CAP'..'CAR').to_a();

puts "#{num_range}";
puts "#{str_range}";

Output

[5, 6, 7, 8]
["CAP", "CAQ", "CAR"]

Explanation

In the above program, we created two ranges num_range, str_range using the range (..) operator and printed the result.

Ruby Basic Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.