From: rringler@... Date: 2018-05-27T20:07:49+00:00 Subject: [ruby-core:87266] [Ruby trunk Bug#14788] `Hash#keys` Could Accept a Block Issue #14788 has been reported by rringler (Ryan Ringler). ---------------------------------------- Bug #14788: `Hash#keys` Could Accept a Block https://bugs.ruby-lang.org/issues/14788 * Author: rringler (Ryan Ringler) * Status: Open * Priority: Normal * Assignee: * Target version: * ruby -v: * Backport: 2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN ---------------------------------------- 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.26 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: