Forum: Ruby an array to string in different lines

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sijo k. (Guest)
on 2009-05-06 13:25
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
Jesús Gabriel y Galán (Guest)
on 2009-05-06 14:30
(Received via mailing list)
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.
This topic is locked and can not be replied to.