Issue #14788 has been updated by jacobevelyn (Jacob Evelyn).


Just noting that I have a similar (but different) proposal in #16739.

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

* Author: rringler (Ryan Ringler)
* Status: Open
* Priority: Normal
----------------------------------------
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>