an array to string in different lines

S

Sijo Kg

Hi
I am collecting errors like object.errors.on:)field) So some times i
get it like an array
["error1_message","error2_message"] or
"error1_message" a string

So how can i format this in more than one line if its an array(or
either case)
example in first case how can i get it like
"error1_message"
"error2_message"

Thanks in advance
Sijo
 
J

Jesús Gabriel y Galán

Hi
=A0 I am collecting errors like object.errors.on:)field) =A0So some times= i
get it like an array
["error1_message","error2_message"] =A0or
"error1_message" a string

=A0 So how can i format this in more than one line =A0if its an array(or
either case)
example in first case how can i get it like
"error1_message"
"error2_message"

A way that works for both cases (array of strings and string):

irb(main):002:0> s =3D ["a", "b"]
=3D> ["a", "b"]
irb(main):003:0> puts [*s].join("\n")
a
b
=3D> nil
irb(main):004:0> s =3D "a"
=3D> "a"
irb(main):005:0> puts [*s].join("\n")
a

Another one:
irb(main):004:0> s =3D "a"
=3D> "a"
irb(main):006:0> puts .flatten.join("\n")
a
=3D> nil
irb(main):007:0> s =3D ["a", "b"]
=3D> ["a", "b"]
irb(main):008:0> puts .flatten.join("\n")
a
b



Hope this helps,

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top