D
Dirk Traulsen
Hi!
Hash#values and Hash#keys return the values/keys of a hash as an array.
Wouldn't it be nice if you could just add a block to specify which
values/keys you want returned?
hash = { 'a' => 1, 'bb' => 2, 'c' => 33, 'dd' => 44}
hash.keys #=> ["a", "bb", "c", "dd"]
hash.values #=> [1, 2, 33, 44]
hash.values {|k,v| k > 'a' } #=> [2, 33, 44]
hash.values {|k,v| v % 2 == 0 } #=> [2, 44]
hash.keys {|k,v| k.size == 1 } #=> ["a", "c"]
Alternatively they could be implemented as new methods called for
example
hash.values_if {}
hash.keys_if {}
analogue to the already defined Hash#values_at.
Well, I would prefer the extension of Hash#keys/values with blocks.
What do you think?
Dirk
Hash#values and Hash#keys return the values/keys of a hash as an array.
Wouldn't it be nice if you could just add a block to specify which
values/keys you want returned?
hash = { 'a' => 1, 'bb' => 2, 'c' => 33, 'dd' => 44}
hash.keys #=> ["a", "bb", "c", "dd"]
hash.values #=> [1, 2, 33, 44]
hash.values {|k,v| k > 'a' } #=> [2, 33, 44]
hash.values {|k,v| v % 2 == 0 } #=> [2, 44]
hash.keys {|k,v| k.size == 1 } #=> ["a", "c"]
Alternatively they could be implemented as new methods called for
example
hash.values_if {}
hash.keys_if {}
analogue to the already defined Hash#values_at.
Well, I would prefer the extension of Hash#keys/values with blocks.
What do you think?
Dirk