From e4a11a1283da07fd1d94535298c605caf299a834 Mon Sep 17 00:00:00 2001 From: Tema Bolshakov Date: Sun, 12 Nov 2023 21:12:16 +0100 Subject: [PATCH] Array#rassoc should try to convert to array implicitly. Fixes #20003 --- array.c | 2 +- test/ruby/test_array.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/array.c b/array.c index a9f4990462..1cbe8b22b9 100644 --- a/array.c +++ b/array.c @@ -5049,7 +5049,7 @@ rb_ary_rassoc(VALUE ary, VALUE value) VALUE v; 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) && RARRAY_LEN(v) > 1 && rb_equal(RARRAY_AREF(v, 1), value)) diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 838ef15b91..558e1c2042 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -529,14 +529,19 @@ class TestArray < Test::Unit::TestCase end def test_assoc + def (a4 = Object.new).to_ary + %w( pork porcine ) + end + a1 = @cls[*%w( cat feline )] a2 = @cls[*%w( dog canine )] a3 = @cls[*%w( mule asinine )] - a = @cls[ a1, a2, a3 ] + a = @cls[ a1, a2, a3, a4 ] assert_equal(a1, a.assoc('cat')) 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('wombat')) assert_equal(nil, a.assoc(1..2)) @@ -1329,13 +1334,17 @@ class TestArray < Test::Unit::TestCase end def test_rassoc + def (a4 = Object.new).to_ary + %w( pork porcine ) + end a1 = @cls[*%w( cat feline )] a2 = @cls[*%w( dog canine )] a3 = @cls[*%w( mule asinine )] - a = @cls[ a1, a2, a3 ] + a = @cls[ a1, a2, a3, a4 ] assert_equal(a1, a.rassoc('feline')) 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('mule')) assert_equal(nil, a.rassoc(1..2))