Home » Ruby programming » Ruby programs

Ruby program to print an array as string

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

Printing an array as string

Given an array and we have to print it as a string in Ruby.

Ruby provides you various alternatives for the single problem. There are plenty of predefined methods which helps you in making your code less complex. The method join is one of them. When we have to print the array elements like a string, join proves its functionality. Another way is also available which facilitates loop and concatenation operator. Let us see both of them in the following code.

join() method

join method is used to join the elements of an array. It can be invoked with parameters after which the parameter passed will be reflected between each join.

Ruby code to print an array as string

=begin 
Ruby program to print an array as string.
=end

# array
arr= Array["Haridwar","Dehradun","Graphic_Era","Includehelp"]

# converting to string 
joinarr1=arr.join(" ") #using join method
# printing
puts joinarr1

#using for loop and .to_s method
joinarr2=""
for i in 0..arr.length
	joinarr2=joinarr2+arr[i].to_s+ " "
end

puts joinarr2

Output

Haridwar Dehradun Graphic_Era Includehelp
Haridwar Dehradun Graphic_Era Includehelp

Code explanation:

In the above code, one can observe that we have invoked join method with no parameters passed because we wanted to keep the join blank. When you scan down, you will observe that for loop is used which is providing the expressions that are concatenating each array element using + operator.



Comments and Discussions!

Load comments ↻





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