T. Onoma
10/10/2004 5:44:00 AM
On Sunday 10 October 2004 12:54 am, Mark Hubbart wrote:
| On Sun, 10 Oct 2004 13:18:48 +0900, trans. (T. Onoma)
|
| <transami@runbox.com> wrote:
| > Word o' warning. I just put this comment in a program:
| >
| > lines = self.split("\n",-1) # boy oh boy was that -1 a pain to figure
| > out!
|
| ???
|
| str = "one\n\ntwo\n\nthree"
| ==>"one\n\ntwo\n\nthree"
| str.split("\n")
| ==>["one", "", "two", "", "three"]
| str.split("\n",-1)
| ==>["one", "", "two", "", "three"]
| RUBY_VERSION
| ==>"1.9.0"
|
| What did you gain by adding the -1 in? I tried this same thing using
| ruby 1.6.8, got the same results.
irb(main):001:0> str = "one\n\ntwo\n\nthree\n\n"
=> "one\n\ntwo\n\nthree\n\n"
irb(main):002:0> str.split("\n")
=> ["one", "", "two", "", "three"]
irb(main):003:0> str.split("\n",-1)
=> ["one", "", "two", "", "three", "", ""]
| Null field filtering is normally only done when no arguments are
| passed, or with the special case " " argument.
Without the -1 I was loosing all my remaining blank lines.
(using 1.8.2)
T.