[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

an array to string in different lines

Sijo Kg

5/6/2009 9:25:00 AM

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
--
Posted via http://www.ruby-....

1 Answer

Jesús Gabriel y Galán

5/6/2009 10:30:00 AM

0

On Wed, May 6, 2009 at 11:25 AM, Sijo Kg <sijo@maxxion.com> wrote:
> 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 [s].flatten.join("\n")
a
=3D> nil
irb(main):007:0> s =3D ["a", "b"]
=3D> ["a", "b"]
irb(main):008:0> puts [s].flatten.join("\n")
a
b



Hope this helps,

Jesus.