mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c (enumerator_with_memo): New method: with_memo().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16795 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3064bb9f8d
commit
a96cf7a7a5
4 changed files with 61 additions and 0 deletions
|
@ -91,6 +91,25 @@ class TestEnumerator < Test::Unit::TestCase
|
|||
assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a)
|
||||
end
|
||||
|
||||
def test_with_memo
|
||||
r = 1..10
|
||||
assert_equal([55, 3628800], (1..10).each.with_memo([0,1]) {|i, memo|
|
||||
memo[0] += i
|
||||
memo[1] *= i
|
||||
})
|
||||
|
||||
a = [2,5,2,1,5,3,4,2,1,0]
|
||||
a.delete_if.with_memo({}) {|i, seen|
|
||||
if seen.key?(i)
|
||||
true
|
||||
else
|
||||
seen[i] = true
|
||||
false
|
||||
end
|
||||
}
|
||||
assert_equal([2, 5, 1, 3, 4, 0], a)
|
||||
end
|
||||
|
||||
def test_next_rewind
|
||||
e = @obj.to_enum(:foo, 1, 2, 3)
|
||||
assert_equal(1, e.next)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue