Forum: Ruby ruby array to list

Posted by robert lengu (robertlengu789)
on 2013-01-29 22:50
Hi
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
I
??
any help will be really good ..
Posted by Stefano Crocco (crocco)
on 2013-01-29 23:02
(Received via mailing list)
On Wednesday 30 January 2013 robert lengu wrote
>
> Posted via http://www.ruby-forum.com/.
Look at Array#join

Stefano
Posted by Ryan Victory (Guest)
on 2013-01-29 23:05
(Received via mailing list)
Sure, with some simple regexes, try out:

<this is a joke>
puts myarray.inspect.to_s.sub(/^\[/, "").sub(/\]$/, "").gsub('"', "")
</this is a joke>

But to be serious, you're looking for the "join" method, like this:

puts myarray.join(", ")

-Ryan Victory
Posted by Jacqueline C. (jacqueline_c)
on 2013-01-29 23:09
(Received via mailing list)
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.
http://www.ruby-doc.org/core-1.9.3/Array.html


Jacqueline Chenault
Web Developer (http://www.adigitalnative.com) | Actor 
(http://www.jacquelinechenault.com)
@ (http://www.twitter.com/adigitalnative)adigitalnative | 
adigitalnative@gmail.com (mailto:adigitalnative@gmail.com) | Facebook 
(http://www.facebook.com/jchenaultacts) | 202.489.6128
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.