How to Convert an Array to a String in Ruby?

Find out how you can convert an array to a string in Ruby

You can use the Array#join method to convert an array to a string, separated by the given separator. For example, to separate each string with a comma and a space (i.e. ", "), you would do the following:

numbers = [1, 2, 3].join(", ")

puts numbers #=> "1, 2, 3"
puts numbers.class #=> String

If no separator is specified, the default separator for Array#join is used (as defined by the $, global variable). If both the separator and $, are nil, then an empty string is used as a separator:

numbers = [1, 2, 3].join

puts numbers #=> "123"
puts numbers.class #=> String

Hope you found this post useful. It was published . Please show your love and support by sharing this post.