From: "David A. Black" Date: 2007-03-17T12:51:17+09:00 Subject: Re: yield Hi -- On 3/16/07, Corey Konrad <0011@hush.com> wrote: > Brian Candler wrote: > > On Sat, Mar 17, 2007 at 07:10:26AM +0900, Corey Konrad wrote: > >> > >> def printer(arg) > >> yield arg > >> end > >> > >> printer("ball") > >> printer {|word| puts word} > >> > >> why is that, cant yield be used with arguments? > > > > It can. Your second function 'printer' takes one argument plus one > > block. > > However in the first case you were calling it without a block, and in > > the > > second case you were calling it without an argument. > > > > What you need is: > > > > printer("ball") {|word| puts word} > > > oh ok, my understanding for some reason was that the method was going to > remmeber that i set arg to "ball" so that when i used printer with the > block that "ball" would still be in arg. So is that why it isnt working, > is because when i call the printer method the second time the value > stored in arg is destroyed? The two calls to printer are unrelated to each other. Each of them uses a local variable 'arg', but they're two different variables. The code block is, syntactically, part of the method call. So if there's no block for a given method call, that's that. David -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)