Skip to content

Commit 7eb570d

Browse files
committed
array.c: return first unique element in Array#uniq
* array.c (ary_add_hash): Fix consistency issue between Array#uniq and Array#uniq! [Bug ruby#9340] [ruby-core:59457] * test/ruby/test_array.rb (class TestArray): regression test for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@44512 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Conflicts: ChangeLog
1 parent 14ab0a4 commit 7eb570d

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

array.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3904,7 +3904,9 @@ ary_add_hash(VALUE hash, VALUE ary)
39043904

39053905
for (i=0; i<RARRAY_LEN(ary); i++) {
39063906
VALUE elt = RARRAY_AREF(ary, i);
3907-
rb_hash_aset(hash, elt, elt);
3907+
if (rb_hash_lookup2(hash, elt, Qundef) == Qundef) {
3908+
rb_hash_aset(hash, elt, elt);
3909+
}
39083910
}
39093911
return hash;
39103912
}

test/ruby/test_array.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,11 @@ def test_uniq
15241524
assert(a.none?(&:frozen?))
15251525
assert_equal(%w(a), b)
15261526
assert(b.none?(&:frozen?))
1527+
1528+
bug9340 = "[ruby-core:59457]"
1529+
ary = [bug9340, bug9340.dup, bug9340.dup]
1530+
assert_equal 1, ary.uniq.size
1531+
assert_same bug9340, ary.uniq[0]
15271532
end
15281533

15291534
def test_uniq_with_block

0 commit comments

Comments
 (0)