Project

General

Profile

Actions

Feature #14788

open

`Hash#keys` Could Accept a Block

Added by rringler (Ryan Ringler) over 3 years ago. Updated over 1 year ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:87266]

Description

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:

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

Proposed:

{ 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) hash_keys_block.patch rringler (Ryan Ringler), 05/28/2018 06:13 AM
Actions

Also available in: Atom PDF