[ruby-core:95791] [Ruby master Feature#16341] Proposal: Set#to_proc
From:
non.dmitriy@...
Date:
2019-11-11 15:18:26 UTC
List:
ruby-core #95791
Issue #16341 has been updated by Nondv (Dmitry Non).
Well, `to_proc` allows to send objects as blocks which can be quite useful not just in case of `select`/`reject`. Also, probably, those two are used more often than `grep`/`grep_v`.
Another example from the top of my head is `count`:
```ruby
dogs = Set[:labrador, :husky, :bullterrier, :corgi]
pets = [:parrot, :labrador, :goldfish, :husky, :labrador, :turtle]
pets.count(&:dogs) # ===> 3
```
----------------------------------------
Feature #16341: Proposal: Set#to_proc
https://bugs.ruby-lang.org/issues/16341#change-82612
* 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:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>