From: Joel VanderWerf Date: 2003-07-17T10:08:11+09:00 Subject: Re: callbacks in ruby and using yield in resursion Paul wrote: > Hi, > > How do I pass a ruby function as an argument to another ruby function so > that it can be used as a callback? > > Also, I have a recursive tree traversing function that I would like to > use yield with. For example: > > def run(parent,level) def run(parent, level, &block) > .... > yield parent block.call(parent) > .... > run(child,level+1)#yield???????? run(child,level+1, &block) > .... > end > > run(root,0){|node| > puts node.get_name > } > > It will not work because i cant use the yield in the recursive call to > run. The first use of the "&block" notation converts the caller-supplied block to an instance of Proc and stores it in "block". The second use of "&block" passes the proc object as the caller-supplied block of the recursive call to #run.