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


I do not feel this proposal to be natural any more. Please close this issue.

----------------------------------------
Feature #10240: `String#to_a`
https://bugs.ruby-lang.org/issues/10240#change-83628

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I often want to exclude the empty strings from an array of strings. A use case is to join an array of strings with `", "`, removing the empty ones, as follows:

```ruby
["Foo", "", "Bar"].reject(&:empty?).join(", ") # => "Foo, Bar"
```

In a similar situation with arrays of arrays, the empty ones can be excluded by using `*` and `Array#to_a`:

```ruby
[*[1, 2], *[], *[3, 4]] # => [1, 2, 3, 4]
```

I would like to propose `String#to_a` defined as follows:

```ruby
class String
  def to_a; empty? ? [] : [self] end
end
```

Then we can do:

```ruby
[*"Foo", *"", *"Bar"].join(", ")
```




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