Array#rassoc should try to convert to array implicitly. Fixes #20003

This commit is contained in:
Tema Bolshakov 2023-11-12 21:12:16 +01:00 коммит произвёл Yusuke Endoh
Родитель 10f44dfeff
Коммит e4a11a1283
2 изменённых файлов: 12 добавлений и 3 удалений

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

@ -5049,7 +5049,7 @@ rb_ary_rassoc(VALUE ary, VALUE value)
VALUE v; VALUE v;
for (i = 0; i < RARRAY_LEN(ary); ++i) { for (i = 0; i < RARRAY_LEN(ary); ++i) {
v = RARRAY_AREF(ary, i); v = rb_check_array_type(RARRAY_AREF(ary, i));
if (RB_TYPE_P(v, T_ARRAY) && if (RB_TYPE_P(v, T_ARRAY) &&
RARRAY_LEN(v) > 1 && RARRAY_LEN(v) > 1 &&
rb_equal(RARRAY_AREF(v, 1), value)) rb_equal(RARRAY_AREF(v, 1), value))

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

@ -529,14 +529,19 @@ class TestArray < Test::Unit::TestCase
end end
def test_assoc def test_assoc
def (a4 = Object.new).to_ary
%w( pork porcine )
end
a1 = @cls[*%w( cat feline )] a1 = @cls[*%w( cat feline )]
a2 = @cls[*%w( dog canine )] a2 = @cls[*%w( dog canine )]
a3 = @cls[*%w( mule asinine )] a3 = @cls[*%w( mule asinine )]
a = @cls[ a1, a2, a3 ] a = @cls[ a1, a2, a3, a4 ]
assert_equal(a1, a.assoc('cat')) assert_equal(a1, a.assoc('cat'))
assert_equal(a3, a.assoc('mule')) assert_equal(a3, a.assoc('mule'))
assert_equal(%w( pork porcine ), a.assoc("pork"))
assert_equal(nil, a.assoc('asinine')) assert_equal(nil, a.assoc('asinine'))
assert_equal(nil, a.assoc('wombat')) assert_equal(nil, a.assoc('wombat'))
assert_equal(nil, a.assoc(1..2)) assert_equal(nil, a.assoc(1..2))
@ -1329,13 +1334,17 @@ class TestArray < Test::Unit::TestCase
end end
def test_rassoc def test_rassoc
def (a4 = Object.new).to_ary
%w( pork porcine )
end
a1 = @cls[*%w( cat feline )] a1 = @cls[*%w( cat feline )]
a2 = @cls[*%w( dog canine )] a2 = @cls[*%w( dog canine )]
a3 = @cls[*%w( mule asinine )] a3 = @cls[*%w( mule asinine )]
a = @cls[ a1, a2, a3 ] a = @cls[ a1, a2, a3, a4 ]
assert_equal(a1, a.rassoc('feline')) assert_equal(a1, a.rassoc('feline'))
assert_equal(a3, a.rassoc('asinine')) assert_equal(a3, a.rassoc('asinine'))
assert_equal(%w( pork porcine ), a.rassoc("porcine"))
assert_equal(nil, a.rassoc('dog')) assert_equal(nil, a.rassoc('dog'))
assert_equal(nil, a.rassoc('mule')) assert_equal(nil, a.rassoc('mule'))
assert_equal(nil, a.rassoc(1..2)) assert_equal(nil, a.rassoc(1..2))