Skip to content

Commit 70a9e5f

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Doc for Hash#transform_keys
1 parent 47d75b6 commit 70a9e5f

File tree

1 file changed

+75
-24
lines changed

1 file changed

+75
-24
lines changed

hash.c

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,40 +3207,91 @@ transform_keys_i(VALUE key, VALUE value, VALUE result)
32073207

32083208
/*
32093209
* call-seq:
3210-
* transform_keys {|key| ... } -> new_hash
3211-
* transform_keys(hash2) -> new_hash
3212-
* transform_keys(hash2) {|other_key| ...} -> new_hash
3210+
* transform_keys {|old_key| ... } -> new_hash
3211+
* transform_keys(other_hash) -> new_hash
3212+
* transform_keys(other_hash) {|old_key| ...} -> new_hash
32133213
* transform_keys -> new_enumerator
32143214
*
3215-
* Returns a new +Hash+ object; each entry has:
3216-
* * A key provided by the block.
3217-
* * The value from +self+.
3215+
* With an argument, a block, or both given,
3216+
* derives a new hash +new_hash+ from +self+, the argument, and/or the block;
3217+
* all, some, or none of its keys may be different from those in +self+.
3218+
*
3219+
* With a block given and no argument,
3220+
* +new_hash+ has keys determined only by the block.
32183221
*
3219-
* An optional hash argument can be provided to map keys to new keys.
3220-
* Any key not given will be mapped using the provided block,
3221-
* or remain the same if no block is given.
3222+
* For each key/value pair <tt>old_key/value</tt> in +self+, calls the block with +old_key+;
3223+
* the block's return value becomes +new_key+;
3224+
* sets <tt>new_hash[new_key] = value</tt>;
3225+
* a duplicate key overwrites:
32223226
*
3223-
* Transform keys:
32243227
* h = {foo: 0, bar: 1, baz: 2}
3225-
* h1 = h.transform_keys {|key| key.to_s }
3226-
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
3228+
* h.transform_keys {|old_key| old_key.to_s }
3229+
* # => {"foo" => 0, "bar" => 1, "baz" => 2}
3230+
* h.transform_keys {|old_key| 'xxx' }
3231+
* # => {"xxx" => 2}
32273232
*
3228-
* h.transform_keys(foo: :bar, bar: :foo)
3229-
* #=> {bar: 0, foo: 1, baz: 2}
3233+
* With argument +other_hash+ given and no block,
3234+
* +new_hash+ may have new keys provided by +other_hash+
3235+
* and unchanged keys provided by +self+.
32303236
*
3231-
* h.transform_keys(foo: :hello, &:to_s)
3232-
* #=> {hello: 0, "bar" => 1, "baz" => 2}
3237+
* For each key/value pair <tt>old_key/old_value</tt> in +self+,
3238+
* looks for key +old_key+ in +other_hash+:
32333239
*
3234-
* Overwrites values for duplicate keys:
3235-
* h = {foo: 0, bar: 1, baz: 2}
3236-
* h1 = h.transform_keys {|key| :bat }
3237-
* h1 # => {bat: 2}
3240+
* - If +old_key+ is found, its value <tt>other_hash[old_key]</tt> is taken as +new_key+;
3241+
* sets <tt>new_hash[new_key] = value</tt>;
3242+
* a duplicate key overwrites:
3243+
*
3244+
* h = {foo: 0, bar: 1, baz: 2}
3245+
* h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO)
3246+
* # => {FOO: 0, BAR: 1, BAZ: 2}
3247+
* h.transform_keys(baz: :FOO, bar: :FOO, foo: :FOO)
3248+
* # => {FOO: 2}
3249+
*
3250+
* - If +old_key+ is not found,
3251+
* sets <tt>new_hash[old_key] = value</tt>;
3252+
* a duplicate key overwrites:
3253+
*
3254+
* h = {foo: 0, bar: 1, baz: 2}
3255+
* h.transform_keys({})
3256+
* # => {foo: 0, bar: 1, baz: 2}
3257+
* h.transform_keys(baz: :foo)
3258+
* # => {foo: 2, bar: 1}
3259+
*
3260+
* Unused keys in +other_hash+ are ignored:
32383261
*
3239-
* Returns a new Enumerator if no block given:
32403262
* h = {foo: 0, bar: 1, baz: 2}
3241-
* e = h.transform_keys # => #<Enumerator: {foo: 0, bar: 1, baz: 2}:transform_keys>
3242-
* h1 = e.each { |key| key.to_s }
3243-
* h1 # => {"foo"=>0, "bar"=>1, "baz"=>2}
3263+
* h.transform_keys(bat: 3)
3264+
* # => {foo: 0, bar: 1, baz: 2}
3265+
*
3266+
* With both argument +other_hash+ and a block given,
3267+
* +new_hash+ has new keys specified by +other_hash+ or by the block,
3268+
* and unchanged keys provided by +self+.
3269+
*
3270+
* For each pair +old_key+ and +value+ in +self+:
3271+
*
3272+
* - If +other_hash+ has key +old_key+ (with value +new_key+),
3273+
* does not call the block for that key;
3274+
* sets <tt>new_hash[new_key] = value</tt>;
3275+
* a duplicate key overwrites:
3276+
*
3277+
* h = {foo: 0, bar: 1, baz: 2}
3278+
* h.transform_keys(baz: :BAZ, bar: :BAR, foo: :FOO) {|key| fail 'Not called' }
3279+
* # => {FOO: 0, BAR: 1, BAZ: 2}
3280+
*
3281+
* - If +other_hash+ does not have key +old_key+,
3282+
* calls the block with +old_key+ and takes its return value as +new_key+;
3283+
* sets <tt>new_hash[new_key] = value</tt>;
3284+
* a duplicate key overwrites:
3285+
*
3286+
* h = {foo: 0, bar: 1, baz: 2}
3287+
* h.transform_keys(baz: :BAZ) {|key| key.to_s.reverse }
3288+
* # => {"oof" => 0, "rab" => 1, BAZ: 2}
3289+
* h.transform_keys(baz: :BAZ) {|key| 'ook' }
3290+
* # => {"ook" => 1, BAZ: 2}
3291+
*
3292+
* With no argument and no block given, returns a new Enumerator.
3293+
*
3294+
* Related: see {Methods for Transforming Keys and Values}[rdoc-ref:Hash@Methods+for+Transforming+Keys+and+Values].
32443295
*/
32453296
static VALUE
32463297
rb_hash_transform_keys(int argc, VALUE *argv, VALUE hash)

0 commit comments

Comments
 (0)