Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Ruby | Range to_a() function

  • Last Updated : 06 Jan, 2020

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 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 program for to_a() 
# method in Range 
  
# Initialize range 
range1 = (7..9)
  
# Prints the array
puts range1.to_a()

Output:

7
8
9
My Personal Notes arrow_drop_up
Recommended Articles
Page :