From 80b537f14c4af699b26e52931ae7e64a547e68c5 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 17 Feb 2016 17:42:09 +0900 Subject: {Enumerable,Array,Range}#first, {Array,Range}#last with block * array.c (rb_ary_first, ary_last): extend Array#{first,last} to accept a block. If a block is passed, these methods collects only elements for which the block returns a truthy value. * enum.c: extend Enumerable#first to accept a block. * range.c: extend Range#{first,last} to accept a block. * gc.c: avoid using rb_ary_last(), because it may call a block. * test/ruby/test_array.rb: add test * test/ruby/test_enum.rb: ditto * test/ruby/test_range.rb: ditto --- test/ruby/test_enum.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test/ruby/test_enum.rb') diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb index ba973e2d48..c230654751 100644 --- a/test/ruby/test_enum.rb +++ b/test/ruby/test_enum.rb @@ -267,6 +267,8 @@ class TestEnumerable < Test::Unit::TestCase assert_equal([1, 2, 3], @obj.first(3)) assert_nil(@empty.first) assert_equal([], @empty.first(10)) + assert_equal(2, @obj.first { |i| i.even? }) + assert_equal([3], @obj.first(2) { |i| i > 2 }) bug5801 = '[ruby-dev:45041]' assert_in_out_err([], <<-'end;', [], /unexpected break/, bug5801) -- cgit v1.2.3