how to write an array in Ruby keeping the elements of the array itself in the same line?
E.g.
a= ["Tom", "Jerry"]
puts a
gives:
Tom
Jerry
But I need to have:
Tom, Jerry
Thank you for your help!
how to write an array in Ruby keeping the elements of the array itself in the same line?
E.g.
a= ["Tom", "Jerry"]
puts a
gives:
Tom
Jerry
But I need to have:
Tom, Jerry
Thank you for your help!
You can use the join
method on the array to do this.
a= ["Tom", "Jerry"]
puts a.join(", ")
Interestingly, you can also multiply the array by the string you wish to separate the elements by:
a= ["Tom", "Jerry"]
puts a * ", "
Both of the above give the same output:
Tom, Jerry