I'm a bit puzzled whether there is any semantic difference between the
two code fragments, below:
# Example from Programming Ruby
def fibUpTo(max)
i1, i2 = 1, 1 # parallel assignment
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end
fibUpTo(1000) { |f| print f, " " }
puts
VS.
# Modified example from Programming Ruby
def fibUpTo(max, blk)
i1, i2 = 1, 1 # parallel assignment
while i1 <= max
blk.call i1
i1, i2 = i2, i1+i2
end
end
fibUpTo(1000, proc { |f| print f, " " })
puts
These seem to be equivalent as far as I can tell. Is the original
example just syntactic sugar for the other version, or are there
subtle reasons why a yield statement and magic associated block (I'm
not sure what you call that) is necessary?
Just curious!
Bob Sidebotham