ADVERTISEMENT
ADVERTISEMENT

Ruby program to demonstrate the range (..) operator

Ruby Example: Write a program to demonstrate the range (..) operator.
Submitted by Nidhi, on December 16, 2021

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!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.