diff options
| author | Kevin Newton <kddnewton@gmail.com> | 2025-11-14 11:31:23 -0500 |
|---|---|---|
| committer | Kevin Newton <kddnewton@gmail.com> | 2025-12-12 13:35:30 -0500 |
| commit | 6147b695870ce82ee3ad5305ce095b63889b8d9d (patch) | |
| tree | 9865c6628b7c5c8543f19a41f2a09f08b2e1475b /test/ruby/test_array.rb | |
| parent | 4f900e3ce9cefa76bc2c94e0e591acd51bf0e638 (diff) | |
Array#rfind
Implement Array#rfind, which is the same as find except from the
other side of the Array. Also implemented Array#find (as opposed to
the generic one on Enumerable because it is significantly faster
and to keep the implementations together.
[Feature #21678]
Diffstat (limited to 'test/ruby/test_array.rb')
| -rw-r--r-- | test/ruby/test_array.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index a3ac0a6a0b..d93b86e795 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -3584,6 +3584,23 @@ class TestArray < Test::Unit::TestCase assert_equal((1..67).to_a.reverse, var_0) end + def test_find + ary = [1, 2, 3, 4, 5] + assert_equal(2, ary.find {|x| x % 2 == 0 }) + assert_equal(nil, ary.find {|x| false }) + assert_equal(:foo, ary.find(proc { :foo }) {|x| false }) + end + + def test_rfind + ary = [1, 2, 3, 4, 5] + assert_equal(4, ary.rfind {|x| x % 2 == 0 }) + assert_equal(1, ary.rfind {|x| x < 2 }) + assert_equal(5, ary.rfind {|x| x > 4 }) + assert_equal(nil, ary.rfind {|x| false }) + assert_equal(:foo, ary.rfind(proc { :foo }) {|x| false }) + assert_equal(nil, ary.rfind {|x| ary.clear; false }) + end + private def need_continuation unless respond_to?(:callcc, true) |
