Home » Ruby programming » Ruby programs

Ruby program to print an array

Printing an array: Here, we are going to learn how to print an array in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on August 16, 2019

Printing an array in Ruby

The task is to develop a program that prints the elements of an array in Ruby programming language.

In Ruby, an array can be printed in many ways depending upon the requirement of the code.

There are several ways to print contents, one of them is using loop which will give you the elements one by one or using print or puts method, the print method will display all the elements in a single row whereas by implementing puts method, all the elements will be printed in different rows.

Methods used:

  • puts: It is used to display the elements in different rows.
  • print: it prints the elements in a single row.

Ruby code to print an arrray

=begin 
Ruby program to print an array
=end

arr= Array["Includehelp","C++","C#","Java"]

#printing single element
puts arr[0]

#printing all the elements in a single row
print arr

#printing one by one using for loop
for i in arr
	puts i
end

#printing one by one without loop
puts arr

Output

Includehelp
["Includehelp", "C++", "C#", "Java"]Includehelp
C++
C#
Java
Includehelp
C++
C#
Java

Explanation:

In the above code, you can observe that we have demonstrated various methods of printing the elements of an array. It depends upon the demand of the user as well as the program code.



Comments and Discussions!

Load comments ↻





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