| Message: 4745 |
 |
BY: Leon Barrett (phaedrus) DATE: 2005-08-17 05:15 SUBJECT: RE: Help Ruby do good Ruby Dangit. I really meant to finish that. Suppose you have a function 'subcase' to easily get your subcases. Then this will work:
def walk(top,&block)
block.call(top)
subcases(top).each { |case| walk(case,&block) }
end
To print all subcases, you can do this:
walk("top"){|top| puts(top)}
Or, to upload them all:
walk("top"){|file| upload(file)}
It's really quite handy. (Note: you can use 'yield' to do the same thing as 'block.call', which means you don't have to include the '&block' bit.) However, in a case like this it's more efficient to keep passing the block. Here's the less efficient version:
def walk(top)
yield(top)
subcases(top).each do |case|
walk(case){|newtop|yield(newtop)
end
end
However, this is less efficient because each subcase is called through a whole chain of blocks. | |