[ruby-core:95819] [Ruby master Feature#16341] Proposal: Set#to_proc
From:
non.dmitriy@...
Date:
2019-11-12 18:58:09 UTC
List:
ruby-core #95819
Issue #16341 has been updated by Nondv (Dmitry Non).
This *is* a syntactic sugar. Using `&` + `to_proc` in this case is the same (not technically, but algorithmically, I guess) as providing an explicit block `something.some_method { |x| some_set.include?(x) }`
I don't find it crucial in any way and, to be honest, I don't really use sets that much (I prefer using hashes directly). But this feature could make some more a tiny bit easier to read from English language perspective (I *think*)
----------------------------------------
Feature #16341: Proposal: Set#to_proc
https://bugs.ruby-lang.org/issues/16341#change-82646
* Author: Nondv (Dmitry Non)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
``` ruby
class Set
def to_proc
-> (x) { include?(x) } # or method(:include?).to_proc
end
end
```
Usage:
```ruby
require 'set'
banned_numbers = Set[0, 5, 7, 9]
(1..10).reject(&banned_numbers) # ===> [1, 2, 3, 4, 6, 8, 10]
```
--
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>