From: zn@... Date: 2018-05-28T16:04:47+00:00 Subject: [ruby-core:87282] [Ruby trunk Feature#14788] `Hash#keys` Could Accept a Block Issue #14788 has been updated by znz (Kazuhiro NISHIYAMA). How about `each_key`? ```ruby { 1 => '1', 2 => '2', 3 => '3', 4 => '4' }.each_key.select(&:odd?) # => [1, 3] ``` ---------------------------------------- Feature #14788: `Hash#keys` Could Accept a Block https://bugs.ruby-lang.org/issues/14788#change-72271 * 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: