diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-02-17 17:42:09 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-04-12 22:17:33 +0900 |
commit | 80b537f14c4af699b26e52931ae7e64a547e68c5 (patch) | |
tree | 17dd1e7babde4c90f27a14988a36bd96b6557c28 /test/ruby/test_range.rb | |
parent | 456523e2ede3073767fd8cb73cc4b159c3608890 (diff) | |
download | ruby-feature/enumerable-first-with-block.tar.gz |
{Enumerable,Array,Range}#first, {Array,Range}#last with blockfeature/enumerable-first-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
Diffstat (limited to 'test/ruby/test_range.rb')
-rw-r--r-- | test/ruby/test_range.rb | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 2fc2a2b1d3..733d45b6f4 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -271,6 +271,10 @@ class TestRange < Test::Unit::TestCase assert_equal("a", ("a".."c").first) assert_equal("c", ("a".."c").last) assert_equal(0, (2..0).last) + assert_equal(1, (0..11).first { |i| i.odd? }) + assert_equal(11, (0..11).last { |i| i.odd? }) + assert_equal([1, 3], (0..11).first(2) { |i| i.odd? }) + assert_equal([9, 11], (0..11).last(2) { |i| i.odd? }) assert_equal([0, 1, 2], (0...10).first(3)) assert_equal([7, 8, 9], (0...10).last(3)) @@ -279,6 +283,10 @@ class TestRange < Test::Unit::TestCase assert_equal("a", ("a"..."c").first) assert_equal("c", ("a"..."c").last) assert_equal(0, (2...0).last) + assert_equal(1, (0...11).first { |i| i.odd? }) + assert_equal(9, (0...11).last { |i| i.odd? }) + assert_equal([1, 3], (0...11).first(2) { |i| i.odd? }) + assert_equal([7, 9], (0...11).last(2) { |i| i.odd? }) end def test_to_s |