parent
135b533e84
commit
1b4d406e3a
13
hash.c
13
hash.c
@ -1549,10 +1549,8 @@ rb_hash_new_with_size(st_index_t size)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
hash_copy(VALUE ret, VALUE hash)
|
||||||
{
|
{
|
||||||
VALUE ret = hash_alloc_flags(klass, flags,
|
|
||||||
RHASH_IFNONE(hash));
|
|
||||||
if (!RHASH_EMPTY_P(hash)) {
|
if (!RHASH_EMPTY_P(hash)) {
|
||||||
if (RHASH_AR_TABLE_P(hash))
|
if (RHASH_AR_TABLE_P(hash))
|
||||||
ar_copy(ret, hash);
|
ar_copy(ret, hash);
|
||||||
@ -1562,6 +1560,13 @@ hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
hash_dup(VALUE hash, VALUE klass, VALUE flags)
|
||||||
|
{
|
||||||
|
return hash_copy(hash_alloc_flags(klass, flags, RHASH_IFNONE(hash)),
|
||||||
|
hash);
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_hash_dup(VALUE hash)
|
rb_hash_dup(VALUE hash)
|
||||||
{
|
{
|
||||||
@ -3213,7 +3218,7 @@ rb_hash_transform_values(VALUE hash)
|
|||||||
VALUE result;
|
VALUE result;
|
||||||
|
|
||||||
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
|
||||||
result = hash_dup(hash, rb_cHash, 0);
|
result = hash_copy(hash_alloc(rb_cHash), hash);
|
||||||
|
|
||||||
if (!RHASH_EMPTY_P(hash)) {
|
if (!RHASH_EMPTY_P(hash)) {
|
||||||
rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, 0);
|
rb_hash_stlike_foreach_with_replace(result, transform_values_foreach_func, transform_values_foreach_replace, 0);
|
||||||
|
@ -1674,9 +1674,15 @@ class TestHash < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_transform_values
|
def test_transform_values
|
||||||
x = @cls[a: 1, b: 2, c: 3]
|
x = @cls[a: 1, b: 2, c: 3]
|
||||||
|
x.default = 42
|
||||||
y = x.transform_values {|v| v ** 2 }
|
y = x.transform_values {|v| v ** 2 }
|
||||||
assert_equal([1, 4, 9], y.values_at(:a, :b, :c))
|
assert_equal([1, 4, 9], y.values_at(:a, :b, :c))
|
||||||
assert_not_same(x, y)
|
assert_not_same(x, y)
|
||||||
|
assert_nil(y.default)
|
||||||
|
|
||||||
|
x.default_proc = proc {|h, k| k}
|
||||||
|
y = x.transform_values {|v| v ** 2 }
|
||||||
|
assert_nil(y.default_proc)
|
||||||
|
|
||||||
y = x.transform_values.with_index {|v, i| "#{v}.#{i}" }
|
y = x.transform_values.with_index {|v, i| "#{v}.#{i}" }
|
||||||
assert_equal(%w(1.0 2.1 3.2), y.values_at(:a, :b, :c))
|
assert_equal(%w(1.0 2.1 3.2), y.values_at(:a, :b, :c))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user