Issue #15562 has been updated by sawa (Tsuyoshi Sawada).


An example of a frequent use case of `split("a", initial_empty_string: false)` is when we have a text like `text` in the following, and want to extract the paragraphs that follow `SECTION`:

```ruby
text = <<~_
  SECTION
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in massa eget mauris lobortis fermentum non in risus. Etiam sit amet dui et velit laoreet pulvinar. Donec convallis, nisi ut lobortis volutpat, est sapien bibendum ante, ac laoreet enim neque at nulla. Aliquam ex urna, porttitor nec mi vitae, suscipit lacinia diam. Maecenas semper, enim id eleifend viverra, lorem velit facilisis tellus, sit amet efficitur nulla nibh sit amet eros. Cras erat mauris, rutrum id mattis nec, auctor eu diam. Aenean mattis at nisl sit amet aliquam. Proin euismod hendrerit eros, quis rhoncus ipsum.

  SECTION
  Curabitur eget quam quis nulla lacinia dapibus ut quis mauris. Maecenas volutpat molestie pulvinar. Mauris porttitor semper arcu. Fusce congue tempor urna in suscipit. Duis a neque lacinia, consectetur elit id, ullamcorper neque. Morbi sit amet eleifend ipsum, sit amet porta libero. Mauris euismod ipsum sit amet ante porttitor consequat. Suspendisse malesuada nunc quis orci posuere dapibus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla quis massa ut tortor pulvinar egestas in ut nunc. Aenean vitae malesuada elit, nec posuere massa. Nullam risus ipsum, fermentum at fringilla eget, tincidunt nec ante. Pellentesque malesuada pulvinar bibendum. Cras massa erat, tristique vitae vehicula et, aliquet vestibulum magna.
_

text.split(/^SECTION\n/, initial_empty_string: false).map(&:strip)
```

----------------------------------------
Feature #15562: `String#split` option to suppress the initial empty substring
https://bugs.ruby-lang.org/issues/15562#change-76507

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
`String#split` returns an empty substring if any at the beginning of the original string, even though it does not return an empty substring at the end of the original string:

```ruby
"aba".split("a") # => ["", "b"]
```

This is probably heritage from Perl or AWK, and may have some use cases, but in some (if not most) use cases, this looks asymmetric, and the initial empty string is unnatural and often requires some additional code to remove it. I propose to give an option to `String#split` to suppress it, perhaps like this (with `true` being the default):

```ruby
"aba".split("a", initial_empty_string: false) # => ["b"]
"aba".split("a", initial_empty_string: true) # => ["", "b"]
"aba".split("ba", initial_empty_string: true) # => ["b"]
```

This does not mean to suppress empty strings in the middle. So it should work like this:

```ruby
"aaaba".split("a", initial_empty_string: false) # => ["", "", "b"]
"aaaba".split("a", initial_empty_string: true) # => ["", "", "", "b"]
```

Or may be we can even go on further to control both the initial and the final ones like (with `:initial` being the default):

```ruby
"aba".split("a", terminal_empty_string: :none) # => ["b"]
"aba".split("a", terminal_empty_string: :initial) # => ["", "b"]
"aba".split("a", terminal_empty_string: :final) # => ["b", ""]
"aba".split("a", terminal_empty_string: :both) # => ["", "b", ""]
```




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>