January 01, 2001
Programming in Ruby
def fibUpTo(max)
n1, n2 = 1, 1
while n1 <= max
yield n1 # invoke block value
n1, n2 = n2, n1+n2 # and calculate next
end
end
Example 5: Iterator for Fibonacci numbers.
|
|
||||||||||||||||||||||||||||
|
|