1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Document Hash#transform_keys with hash. Amend NEWS [DOC] [ci skip]

This commit is contained in:
Marc-Andre Lafortune 2020-12-15 17:05:35 -05:00
parent d5f0d338c7
commit 8558d5e480
2 changed files with 17 additions and 16 deletions

View file

@ -196,8 +196,8 @@ Outstanding ones only.
* Hash * Hash
* Hash#transform_keys now accepts a hash that maps keys to new * Hash#transform_keys and transform_keys! now accepts a hash that maps
keys. [[Feature #16274]] keys to new keys. [[Feature #16274]]
* Hash#except has been added, which returns a hash excluding the * Hash#except has been added, which returns a hash excluding the
given keys and their values. [[Feature #15822]] given keys and their values. [[Feature #15822]]

29
hash.c
View file

@ -3188,17 +3188,28 @@ transform_keys_i(VALUE key, VALUE value, VALUE result)
* call-seq: * call-seq:
* hash.transform_keys {|key| ... } -> new_hash * hash.transform_keys {|key| ... } -> new_hash
* hash.transform_keys(hash2) -> new_hash * hash.transform_keys(hash2) -> new_hash
* hash.transform_keys(hash2) {|other_key| ...} -> new_hash
* hash.transform_keys -> new_enumerator * hash.transform_keys -> new_enumerator
* *
* Returns a new \Hash object; each entry has: * Returns a new \Hash object; each entry has:
* * A key provided by the block. * * A key provided by the block.
* * The value from +self+. * * The value from +self+.
* *
* An optional hash argument can be provided to map keys to new keys.
* Any key not given will be mapped using the provided block,
* or remain the same if no block is given.
*
* Transform keys: * Transform keys:
* h = {foo: 0, bar: 1, baz: 2} * h = {foo: 0, bar: 1, baz: 2}
* h1 = h.transform_keys {|key| key.to_s } * h1 = h.transform_keys {|key| key.to_s }
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2} * h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
* *
* h.transform_keys(foo: :bar, bar: :foo)
* #=> {bar: 0, foo: 1, baz: 2}
*
* h.transform_keys(foo: :hello, &:to_s)
* #=> {:hello=>0, "bar"=>1, "baz"=>2}
*
* Overwrites values for duplicate keys: * Overwrites values for duplicate keys:
* h = {foo: 0, bar: 1, baz: 2} * h = {foo: 0, bar: 1, baz: 2}
* h1 = h.transform_keys {|key| :bat } * h1 = h.transform_keys {|key| :bat }
@ -3243,22 +3254,12 @@ static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash);
/* /*
* call-seq: * call-seq:
* hash.transform_keys! {|key| ... } -> self * hash.transform_keys! {|key| ... } -> self
* hash.transform_keys!(hash2) -> self
* hash.transform_keys!(hash2) {|other_key| ...} -> self
* hash.transform_keys! -> new_enumerator * hash.transform_keys! -> new_enumerator
* *
* Returns +self+ with new keys provided by the block: * Same as Hash#transform_keys but modifies the receiver in place
* h = {foo: 0, bar: 1, baz: 2} * instead of returning a new hash.
* h.transform_keys! {|key| key.to_s } # => {"foo"=>0, "bar"=>1, "baz"=>2}
*
* Overwrites values for duplicate keys:
* h = {foo: 0, bar: 1, baz: 2}
* h1 = h.transform_keys! {|key| :bat }
* h1 # => {:bat=>2}
*
* Returns a new \Enumerator if no block given:
* h = {foo: 0, bar: 1, baz: 2}
* e = h.transform_keys! # => #<Enumerator: {"foo"=>0, "bar"=>1, "baz"=>2}:transform_keys!>
* h1 = e.each { |key| key.to_s }
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
*/ */
static VALUE static VALUE
rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash) rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)