[ruby-core:96657] [Ruby master Feature#10240] `String#to_a`
[email protected] Sat, 04 Jan 2020 18:21:58 +0000 (UTC)
| Newsgroups | gmane.comp.lang.ruby.core |
|---|---|
| Message-ID | <redmine.journal-83628.20200104182157.253f25f6c966e6c0@ruby-lang.org> |
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:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>