Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby array to string conversion

People also ask

How do I turn an array into a string in Ruby?

In Ruby, we can convert an array into a string using the join method. The join method takes the array and a separator as the arguments. It then separates the elements in the array using the specified separator value.

What is TO_S in Ruby?

The to_s function in Ruby returns a string containing the place-value representation of int with radix base (between 2 and 36). If no base is provided in the parameter then it assumes the base to be 10 and returns.


I'll join the fun with:

['12','34','35','231'].join(', ')

EDIT:

"'#{['12','34','35','231'].join("', '")}'"

Some string interpolation to add the first and last single quote :P


> a = ['12','34','35','231']
> a.map { |i| "'" + i.to_s + "'" }.join(",")
=> "'12','34','35','231'"

try this code ['12','34','35','231']*","

will give you result "12,34,35,231"

I hope this is the result you, let me know


array.map{ |i|  %Q('#{i}') }.join(',')