An array to string in different lines


#1

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


#2

On Wed, May 6, 2009 at 11:25 AM, Sijo Kg removed_email_address@domain.invalid wrote:

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

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

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

Hope this helps,

Jesus.