From: non.dmitriy@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:95823] [Ruby master Feature#16341] Proposal: Set#to_proc and Hash#to_proc
Date: Tue, 12 Nov 2019 20:12:55 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-82652.20191112201254.9ca31c994ef1c643@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16341.20191111150227@ruby-lang.org
Issue #16341 has been updated by Nondv (Dmitry Non).
shan (Shannon Skipper) wrote:
> This already works!
I can't believe I'm so oblivious :D
----------------------------------------
Feature #16341: Proposal: Set#to_proc and Hash#to_proc
https://bugs.ruby-lang.org/issues/16341#change-82652
* 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]
```
**UPD**
also for hash:
```ruby
class Hash
def to_proc
->(key) { self[key] }
end
end
dogs = ['Lucky', 'Tramp', 'Lady']
favourite_food = { 'Lucky' => 'salmon', 'Tramp' => 'pasta', 'Lady' => 'pasta' }
food_to_order = dogs.map(&favourite_food)
```
--
https://bugs.ruby-lang.org/
prev parent reply other threads:[~2019-11-12 20:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-16341.20191111150227@ruby-lang.org>
2019-11-11 15:02 ` [ruby-core:95789] [Ruby master Bug#16341] Proposal: Set#to_proc non.dmitriy
2019-11-11 15:08 ` [ruby-core:95790] [Ruby master Feature#16341] " zverok.offline
2019-11-11 15:18 ` [ruby-core:95791] " non.dmitriy
2019-11-11 15:26 ` [ruby-core:95793] " zverok.offline
2019-11-11 15:37 ` [ruby-core:95794] " non.dmitriy
2019-11-11 23:43 ` [ruby-core:95804] " shevegen
2019-11-12 18:58 ` [ruby-core:95819] " non.dmitriy
2019-11-12 19:05 ` [ruby-core:95820] " non.dmitriy
2019-11-12 19:09 ` [ruby-core:95821] " non.dmitriy
2019-11-12 19:44 ` [ruby-core:95822] [Ruby master Feature#16341] Proposal: Set#to_proc and Hash#to_proc shannonskipper
2019-11-12 20:12 ` non.dmitriy [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-82652.20191112201254.9ca31c994ef1c643@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).