[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: [rcr] String#split behaves odd

Peña, Botp

12/8/2004 5:52:00 AM

Yukihiro Matsumoto [mailto:matz@ruby-lang.org] wrote:
//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


1 Answer

Yukihiro Matsumoto

12/8/2004 5:57:00 AM

0

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. ;-)
I owe him too much to do it by myself.
matz.