hash.c: rb_hash_reject without dup

* hash.c (rb_hash_reject): copy unrejected elements only to new hash,
  so that the change on the original receiver can affect.
  [ruby-core:58914] [Bug #9223]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44047 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-12-07 02:16:04 +00:00
Родитель 77280b6cd6
Коммит 7416073cde
3 изменённых файлов: 30 добавлений и 4 удалений

Просмотреть файл

@ -1,3 +1,9 @@
Sat Dec 7 11:15:52 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c (rb_hash_reject): copy unrejected elements only to new hash,
so that the change on the original receiver can affect.
[ruby-core:58914] [Bug #9223]
Sat Dec 7 08:25:00 2013 Richo Healey <richo@psych0tik.net> Sat Dec 7 08:25:00 2013 Richo Healey <richo@psych0tik.net>
* test/ruby/test_struct.rb: Add regression test for question marks and * test/ruby/test_struct.rb: Add regression test for question marks and

18
hash.c
Просмотреть файл

@ -1092,6 +1092,15 @@ rb_hash_reject_bang(VALUE hash)
return hash; return hash;
} }
static int
reject_i(VALUE key, VALUE value, VALUE hash)
{
if (!RTEST(rb_yield_values(2, key, value))) {
rb_hash_aset(hash, key, value);
}
return ST_CONTINUE;
}
/* /*
* call-seq: * call-seq:
* hsh.reject {| key, value | block } -> a_hash * hsh.reject {| key, value | block } -> a_hash
@ -1106,7 +1115,14 @@ rb_hash_reject_bang(VALUE hash)
static VALUE static VALUE
rb_hash_reject(VALUE hash) rb_hash_reject(VALUE hash)
{ {
return rb_hash_delete_if(rb_obj_dup(hash)); VALUE ret;
RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size);
ret = hash_alloc(rb_obj_class(hash));
if (!RHASH_EMPTY_P(hash)) {
rb_hash_foreach(hash, reject_i, ret);
}
return ret;
} }
/* /*

Просмотреть файл

@ -501,9 +501,13 @@ class TestEnumerator < Test::Unit::TestCase
def test_size_for_enum_created_from_hash def test_size_for_enum_created_from_hash
h = {a: 1, b: 2, c: 3} h = {a: 1, b: 2, c: 3}
%i[delete_if reject! select select! keep_if each each_key each_pair].each do |method| methods = %i[delete_if reject reject! select select! keep_if each each_key each_pair]
assert_equal 3, h.send(method).size enums = methods.map {|method| h.send(method)}
end s = enums.group_by(&:size)
assert_equal([3], s.keys, ->{s.reject!{|k| k==3}.inspect})
h[:d] = 4
s = enums.group_by(&:size)
assert_equal([4], s.keys, ->{s.reject!{|k| k==4}.inspect})
end end
def test_size_for_enum_created_from_env def test_size_for_enum_created_from_env