Ruby array to list

I dont know if i can do this or is there is any way to implement the
logic or i am just being stupid…

suppose, if i do this

p myarray

it give me

[“test”, “test3”, “test4”, “test5”]

but is there any way to make this output like bellow

test, test3, test4, test5
any help will be really good …

On Wednesday 30 January 2013 robert lengu wrote

Posted via
Look at Array#join


Sure, with some simple regexes, try out:

puts myarray.inspect.to_s.sub(/^\[/, "").sub(/\]$/, "").gsub('"', "")

But to be serious, you’re looking for the “join” method, like this:

puts myarray.join(", ")

-Ryan V.

Robert, try

myarray.join(", ")

It will take every item in the array and join it using what you put in
parentheses. It returns a string, so what you’ll really get back is
“test1, test3, test4, test5”

If you look at the Ruby docs for Arrays you’ll find some other useful
methods you can call on them.

Jacqueline Chenault
Web D. ( | Actor
@ ( |
[email protected] (mailto:[email protected]) | Facebook
(Jacqueline Chenault*) | 202.489.6128