summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-14 06:44:00 (GMT)
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-07-14 06:44:00 (GMT)
commit1405111722af070f443e32d31d96177dac21b782 (patch)
tree676151764f3753acba21e2dd6114c2a8a3de33f9 /hash.c
parent0b396d588060707e227f21b80e964180674c8a50 (diff)
hash.c: Add Hash#transform_keys and Hash#transform_keys!
* hash.c (transform_keys_i, rb_hash_transform_keys): Add Hash#transform_keys. [Feature #13583] [ruby-core:81290] * hash.c (rb_hash_transform_keys_bang): Add Hash#transform_keys!. [Feature #13583] [ruby-core:81290] * test/ruby/test_hash.rb: Add tests for above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59328 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/hash.c b/hash.c
index 7a0beb7..3918fdd 100644
--- a/hash.c
+++ b/hash.c
@@ -1825,6 +1825,78 @@ rb_hash_each_pair(VALUE hash)
}
static int
+transform_keys_i(VALUE key, VALUE value, VALUE result)
+{
+ VALUE new_key = rb_yield(key);
+ rb_hash_aset(result, new_key, value);
+ return ST_CONTINUE;
+}
+
+/*
+ * call-seq:
+ * hsh.transform_keys {|key| block } -> new_hash
+ * hsh.transform_keys -> an_enumerator
+ *
+ * Returns a new hash with the results of running the block once for
+ * every key.
+ * This method does not change the values.
+ *
+ * h = { a: 1, b: 2, c: 3 }
+ * h.transform_keys {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
+ * h.transform_keys(&:to_s) #=> { "a" => 1, "b" => 2, "c" => 3 }
+ * h.transform_keys.with_index {|k, i| "#{k}.#{i}" }
+ * #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
+ *
+ * If no block is given, an enumerator is returned instead.
+ */
+static VALUE
+rb_hash_transform_keys(VALUE hash)
+{
+ VALUE result;
+
+ RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
+ result = rb_hash_new();
+ if (!RHASH_EMPTY_P(hash)) {
+ rb_hash_foreach(hash, transform_keys_i, result);
+ }
+
+ return result;
+}
+
+/*
+ * call-seq:
+ * hsh.transform_keys! {|value| block } -> hsh
+ * hsh.transform_keys! -> an_enumerator
+ *
+ * Invokes the given block once for each key in <i>hsh</i>, replacing it
+ * with the new key returned by the block, and then returns <i>hsh</i>.
+ * This method does not change the values.
+ *
+ * h = { a: 1, b: 2, c: 3 }
+ * h.transform_keys! {|k| k.to_s } #=> { "a" => 1, "b" => 2, "c" => 3 }
+ * h.transform_keys!(&:to_sym) #=> { a: 1, b: 2, c: 3 }
+ * h.transform_keys!.with_index {|k, i| "#{k}.#{i}" }
+ * #=> { "a.0" => 1, "b.1" => 2, "c.2" => 3 }
+ *
+ * If no block is given, an enumerator is returned instead.
+ */
+static VALUE
+rb_hash_transform_keys_bang(VALUE hash)
+{
+ RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
+ rb_hash_modify_check(hash);
+ if (RHASH(hash)->ntbl) {
+ long i;
+ VALUE keys = rb_hash_keys(hash);
+ for (i = 0; i < RARRAY_LEN(keys); ++i) {
+ VALUE new_key = rb_yield(RARRAY_AREF(keys, i));
+ rb_hash_aset(hash, new_key, rb_hash_delete(hash, RARRAY_AREF(keys, i)));
+ }
+ }
+ return hash;
+}
+
+static int
transform_values_i(VALUE key, VALUE value, VALUE result)
{
VALUE new_value = rb_yield(value);
@@ -4507,6 +4579,8 @@ Init_Hash(void)
rb_define_method(rb_cHash, "each_pair", rb_hash_each_pair, 0);
rb_define_method(rb_cHash, "each", rb_hash_each_pair, 0);
+ rb_define_method(rb_cHash, "transform_keys", rb_hash_transform_keys, 0);
+ rb_define_method(rb_cHash, "transform_keys!", rb_hash_transform_keys_bang, 0);
rb_define_method(rb_cHash, "transform_values", rb_hash_transform_values, 0);
rb_define_method(rb_cHash, "transform_values!", rb_hash_transform_values_bang, 0);