Assignments
2 Comments »
This page intentionally left blank.Just kidding. This page is only here to make that neat outline in the right-hand column that lists all of the assignments. I should probably provide links here, but you can just use the ones over there –>
September 29th, 2007 at 9:15 pm
I’m not very sure where to ask this question, so I’ll ask this here. If there is a better place, please someone let me know. The question is about “yield”.
In lecture 2, can there be more than 1 yield in a function that would respond to different blocks? For example:
def compute
for i in [1, 2, 3]
if block_given?
yield i #yield logic A
end
end
for j in [1, 2, 3, 4]
if block_given?
yield j #for yield logic B
end
end
end
> compute {|i| puts “Logic A for #{i}”} {|j| puts “Logic B for #{j}” }
thanks
September 29th, 2007 at 11:48 pm
yield passes its arguments to the single block that is given with braces ({}) or with do end.
In other words, there is no syntax to pass directly two blocks to some method (say, compute) and give a method such as your ‘compute’ the ability to yield to one, or the other, block.
So the simple answer is: No.
Note that in a later lecture we will take about passing a block _as a parameter_. You can pass any number of block as parameters, and pass arguments to any of those blocks. But we’re not there yet. It is also worth noting that most problems can be solved with simple constructs. Attend to the simple and direct solution.
Keep it simple!
“There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies.” — C. A. R. Hoare