Splitting a string separated by CRLF into array

How can I get the array

["hello", "world", "123"]

from the string

"hello\r\nworld\r\n123"

Thanks!
Dominique

Hi --

···

On Sat, 22 Jul 2006, dominique.plante@gmail.com wrote:

How can I get the array

["hello", "world", "123"]

from the string

"hello\r\nworld\r\n123"

str.split("\r\n")

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

irb(main):001:0> "hello\r\nworld\r\n123".split
=> ["hello", "world", "123"]

irb(main):011:0> "hello\r\nworld\r\n123".split(/\s+/)
=> ["hello", "world", "123"]

irb(main):012:0> "hello\r\nworld\r\n123".scan(/\S+/)
=> ["hello", "world", "123"]

irb(main):004:0> "hello\r\nworld\r\n123".to_a.map{|x|x.chomp}
=> ["hello", "world", "123"]

irb(main):009:0> "hello\r\nworld\r\n123".inject(){|a,b| a << b.chomp}
=> ["hello", "world", "123"]

···

dominique.plante@gmail.com wrote:

How can I get the array

["hello", "world", "123"]

from the string

"hello\r\nworld\r\n123"

Thanks!
Dominique

Thanks! (I like your book BTW - I like how it touches on main, Kernel
stuff, etc...)

I was trying "hello\r\n\world".split('\r\n') and it returned
["hello\r\nworld"]

dblack@wobblini.net wrote:

···

Hi --

On Sat, 22 Jul 2006, dominique.plante@gmail.com wrote:

> How can I get the array
>
> ["hello", "world", "123"]
>
> from the string
>
> "hello\r\nworld\r\n123"

str.split("\r\n")

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
Ruby for Rails => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
dblack@wobblini.net => me

You need to use a double quoted string in order for those escaped characters to be interpereted as such.

Tom

···

dominique.plante@gmail.com wrote:

Thanks! (I like your book BTW - I like how it touches on main, Kernel
stuff, etc...)

I was trying "hello\r\n\world".split('\r\n') and it returned
["hello\r\nworld"]

--
Tom Werner
Helmets to Hardhats
Software Developer
tom@helmetstohardhats.org
www.helmetstohardhats.org