Bug #9202 ยป 0001-Array-uniq-uniq-does-not-return-frozen-hash-keys.patch
array.c | ||
---|---|---|
long i;
|
||
for (i=0; i<RARRAY_LEN(ary); i++) {
|
||
rb_hash_aset(hash, RARRAY_AREF(ary, i), Qtrue);
|
||
VALUE elt = RARRAY_AREF(ary, i);
|
||
rb_hash_aset(hash, elt, elt);
|
||
}
|
||
return hash;
|
||
}
|
||
... | ... | |
ary2 = to_ary(ary2);
|
||
hash = ary_add_hash(ary_make_hash(ary1), ary2);
|
||
ary3 = rb_hash_keys(hash);
|
||
ary3 = rb_hash_values(hash);
|
||
ary_recycle_hash(hash);
|
||
return ary3;
|
||
}
|
||
static int
|
||
push_key(st_data_t key, st_data_t val, st_data_t ary)
|
||
push_val(st_data_t key, st_data_t val, st_data_t ary)
|
||
{
|
||
rb_ary_push((VALUE)ary, (VALUE)key);
|
||
rb_ary_push((VALUE)ary, (VALUE)val);
|
||
return ST_CONTINUE;
|
||
}
|
||
... | ... | |
FL_SET_EMBED(ary);
|
||
}
|
||
ary_resize_capa(ary, hash_size);
|
||
st_foreach(rb_hash_tbl_raw(hash), push_key, ary);
|
||
st_foreach(rb_hash_tbl_raw(hash), push_val, ary);
|
||
}
|
||
ary_recycle_hash(hash);
|
||
... | ... | |
}
|
||
else {
|
||
hash = ary_make_hash(ary);
|
||
uniq = rb_hash_keys(hash);
|
||
uniq = rb_hash_values(hash);
|
||
}
|
||
RBASIC_SET_CLASS(uniq, rb_obj_class(ary));
|
||
ary_recycle_hash(hash);
|
test/ruby/test_array.rb | ||
---|---|---|
assert_equal(d, c)
|
||
assert_equal(@cls[1, 2, 3], @cls[1, 2, 3].uniq)
|
||
a = %w(a a)
|
||
b = a.uniq
|
||
assert_equal(%w(a a), a)
|
||
assert(a.none?(&:frozen?))
|
||
assert_equal(%w(a), b)
|
||
assert(b.none?(&:frozen?))
|
||
end
|
||
def test_uniq_with_block
|
||
... | ... | |
assert_equal([1,3], a)
|
||
assert_equal([1], b)
|
||
assert_not_same(a, b)
|
||
a = %w(a a)
|
||
b = a.uniq {|v| v }
|
||
assert_equal(%w(a a), a)
|
||
assert(a.none?(&:frozen?))
|
||
assert_equal(%w(a), b)
|
||
assert(b.none?(&:frozen?))
|
||
end
|
||
def test_uniq!
|
||
... | ... | |
a.sort_by!{|e| e[:c]}
|
||
a.uniq! {|e| e[:c]}
|
||
end
|
||
a = %w(a a)
|
||
b = a.uniq
|
||
assert_equal(%w(a a), a)
|
||
assert(a.none?(&:frozen?))
|
||
assert_equal(%w(a), b)
|
||
assert(b.none?(&:frozen?))
|
||
end
|
||
def test_uniq_bang_with_block
|
||
... | ... | |
b = a.uniq! {|v| v.even? }
|
||
assert_equal([1,2], a)
|
||
assert_equal(nil, b)
|
||
a = %w(a a)
|
||
b = a.uniq! {|v| v }
|
||
assert_equal(%w(a), b)
|
||
assert_same(a, b)
|
||
assert b.none?(&:frozen?)
|
||
end
|
||
def test_uniq_bang_with_freeze
|
||
... | ... | |
assert_equal(@cls[1,2], @cls[1] | @cls[2])
|
||
assert_equal(@cls[1,2], @cls[1, 1] | @cls[2, 2])
|
||
assert_equal(@cls[1,2], @cls[1, 2] | @cls[1, 2])
|
||
a = %w(a b c)
|
||
b = %w(a b c d e)
|
||
c = a | b
|
||
assert_equal(c, b)
|
||
assert_not_same(c, b)
|
||
assert_equal(%w(a b c), a)
|
||
assert_equal(%w(a b c d e), b)
|
||
assert(a.none?(&:frozen?))
|
||
assert(b.none?(&:frozen?))
|
||
assert(c.none?(&:frozen?))
|
||
end
|
||
def test_combination
|