[rcr] String#split behaves odd

//I feel it might be difficult. Define the following:
//
// "abc\n".split("\n").join("\n")

I thought we can cater that w a limit parameter,

irb(main):040:0> "\nabc\ndef\n".split("\n",-1).join("\n")
=> "\nabc\ndef\n"

and that split by itself (w/o limit param) simplifies by trimming trailing
edges -which is good in some cases, imho.

My remainng concern is [] vs [""] thingy. Both behave similarly w split and
joins, yet I prefer the [""]. But that is just me.

irb(main):046:0> "a".split("b")
=> ["a"]
irb(main):047:0> "".split("b")
=> [] #<=== I was hoping for [""] here

No worry here, this just very small concern.

irb(main):065:0> [""].collect{|x| x+"!"}
=>["!"]
irb(main):066:0> [].collect{|x| x+"!"}
=>[] #<== [] acts like blackhole

Thank you, sir Matz.

//
// matz.

kind regards -botp

···

Yukihiro Matsumoto [mailto:matz@ruby-lang.org] wrote:

Hi,

···

In message "Re: [rcr] String#split behaves odd" on Wed, 8 Dec 2004 14:52:24 +0900, "Peña, Botp" <botp@delmonte-phil.com> writes:

I thought we can cater that w a limit parameter,

irb(main):040:0> "\nabc\ndef\n".split("\n",-1).join("\n")
=> "\nabc\ndef\n"

and that split by itself (w/o limit param) simplifies by trimming trailing
edges -which is good in some cases, imho.

Blame Larry Wall for the weird default behavior. :wink:
I owe him too much to do it by myself.
              matz.