Ruby iterators now automatically return an Enumerator if no block is given - allows very cool code ..

This commit is contained in:
Matthias Koefferlein
2020-08-31 23:09:37 +02:00
parent a5d675304c
commit 4c127b4644
4 changed files with 50 additions and 4 deletions
+10
View File
@@ -733,6 +733,8 @@ class Basic_TestClass < TestBase
arr = []
b.each_b_copy { |bb| arr.push(bb.b2) }
assert_equal(arr, ["a", "y", "uu"])
# through enumerator
assert_equal(b.each_b_copy.collect(&:b2), ["a", "y", "uu"])
arr = []
b.each_b_copy { |bb| bb.b5(bb.b2 + "x"); arr.push(bb.b2) }
@@ -745,6 +747,8 @@ class Basic_TestClass < TestBase
arr = []
b.each_b_cref { |bb| arr.push(bb.b2) }
assert_equal(arr, ["a", "y", "uu"])
# through enumerator
assert_equal(b.each_b_cref.collect(&:b2), ["a", "y", "uu"])
arr = []
# this works, since the "const B &" will be converted to a copy
@@ -759,6 +763,8 @@ class Basic_TestClass < TestBase
arr = []
b.each_b_cptr { |bb| arr.push(bb.b2) }
assert_equal(arr, ["a", "y", "uu"])
# through enumerator
assert_equal(b.each_b_cptr.collect(&:b2), ["a", "y", "uu"])
arr = []
# const references cannot be modified
@@ -778,6 +784,8 @@ class Basic_TestClass < TestBase
arr = []
b.each_b_ref { |bb| arr.push(bb.b2) }
assert_equal(arr, ["a", "y", "uu"])
# through enumerator
assert_equal(b.each_b_ref.collect(&:b2), ["a", "y", "uu"])
arr = []
b.each_b_ref { |bb| bb.b5(bb.b2 + "x"); arr.push(bb.b2) }
@@ -790,6 +798,8 @@ class Basic_TestClass < TestBase
arr = []
b.each_b_ptr { |bb| arr.push(bb.b2) }
assert_equal(arr, ["ax", "yx", "uux"])
# through enumerator
assert_equal(b.each_b_ptr.collect(&:b2), ["ax", "yx", "uux"])
arr = []
b.each_b_ptr { |bb| bb.b5(bb.b2 + "x"); arr.push(bb.b2) }
+4
View File
@@ -67,6 +67,8 @@ class DBPolygon_TestClass < TestBase
arr = []
a.each_point_hull { |p| arr.push( p.to_s ) }
assert_equal( arr, ["5,-10", "5,15", "20,15", "20,-10"] )
# with enumerator
assert_equal( a.each_point_hull.collect(&:to_s), ["5,-10", "5,15", "20,15", "20,-10"] )
b = a.dup
@@ -130,6 +132,8 @@ class DBPolygon_TestClass < TestBase
arr = []
a.each_point_hole(0) { |p| arr.push( p.to_s ) }
assert_equal( arr, ["1,2", "2,2", "2,6"] )
# with enumerator
assert_equal( a.each_point_hole(0).collect(&:to_s), ["1,2", "2,2", "2,6"] )
arr = []
a.each_edge { |p| arr.push( p.to_s ) }