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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




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.