From: sawadatsuyoshi@...
Date: 2018-05-28T07:35:09+00:00
Subject: [ruby-core:87272] [Ruby trunk Feature#14788] `Hash#keys` Could	Accept a Block

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


If I were you, I would rather request a new method named `Hash#select_keys` for that purpose.

----------------------------------------
Feature #14788: `Hash#keys` Could Accept a Block
https://bugs.ruby-lang.org/issues/14788#change-72262

* Author: rringler (Ryan Ringler)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Sometimes I only need to fetch *some* of the keys from a Hash. With the current `Hash#keys` implementation, this requires fetching all the keys and then selecting the ones I'm interested in. It would be nice if `Has#keys` accepted a block, and only returned the keys for which the block evaluated to true.

**Currently:**
~~~ruby
{ 1 => '1', 2 => '2', 3 => '3', 4 => '4' }.keys.select { |key| key.odd? } # => [1, 3]
~~~

**Proposed:**
~~~ruby
{ 1 => '1', 2 => '2', 3 => '3', 4 => '4' }.keys { |key| key.odd? } # => [1, 3]
~~~

The attached patch shows how `rb_hash_keys` might be modified to check for a passed block.

---Files--------------------------------
hash_keys_block.patch (2.27 KB)


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