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


> (shevegen (Robert A. Heiler):) This reminds me a bit of Dir['*'] versus Dir.entries(Dir.pwd). The latter
also has . and .. entries

Actually, I had the same thing in mind. I have never felt the initial `""` in `String#split` useful (as well as the `.` and `..` in `Dir[]`). They are along the same lines to me.

And I agree with knu that the name for the option was too long. I had felt that too. So I came up with a different name. What about `leader`?

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



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

* 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>