summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-11 09:49:14 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-03-11 09:49:14 +0000
commitc2d3b8dfb43e6dbc0b9af3c0f880636ffcc06ca9 (patch)
tree3ddc67451c963d775f07eb9f1092fed7974a58bb /test/ruby
parent7d9664953ba574b04f6558e80e95a71289275d64 (diff)
Implement Enumerator::Yielder#to_proc
A Yielder object can now be directly passed to another method as a block argument. ```ruby enum = Enumerator.new { |y| Dir.glob("*.rb") { |file| File.open(file) { |f| f.each_line(&y) } } } ``` git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67211 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_enumerator.rb15
1 files changed, 15 insertions, 0 deletions
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index efcfef580f..5d1e8f05b1 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -493,6 +493,21 @@ class TestEnumerator < Test::Unit::TestCase
assert_equal([1, 2, 3], y.yield(2, 3))
assert_raise(LocalJumpError) { Enumerator::Yielder.new }
+
+ # to_proc (explicit)
+ a = []
+ y = Enumerator::Yielder.new {|x| a << x }
+ b = y.to_proc
+ assert_kind_of(Proc, b)
+ assert_equal([1], b.call(1))
+ assert_equal([1], a)
+
+ # to_proc (implicit)
+ e = Enumerator.new { |y|
+ assert_kind_of(Enumerator::Yielder, y)
+ [1, 2, 3].each(&y)
+ }
+ assert_equal([1, 2, 3], e.to_a)
end
def test_size