Open In App

Ruby | Range to_a() function

Last Updated : 19 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
The to_a() is an inbuilt method in Ruby returns an array containing the numbers in the given range.
Syntax: range1.to_a() Parameters: The function accepts no parameter. Return Value: It returns an array containing all the numbers.
Example 1: Ruby
# Ruby program for to_a() 
# method in Range 

# Initialize range 
range1 = (0..4)

# Prints the array
puts range1.to_a()
Output:
0
1
2
3
4
Example 2: Ruby
# Ruby program for to_a() 
# method in Range 

# Initialize range 
range1 = (7..9)

# Prints the array
puts range1.to_a()
Output:
7
8
9

Next Article

Similar Reads