From: "maillist@..." Date: 2003-07-17T10:59:31+09:00 Subject: Re: callbacks in ruby and using yield in resursion It works! Thanks very much. One thing I don't get... what exactly is the &block, is it a reserved work, a predefined variable or what??? On Thu, 2003-07-17 at 13:08, Joel VanderWerf wrote: > 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. >