From: Chad Perrin Date: 2007-03-17T07:42:31+09:00 Subject: Re: yield On Sat, Mar 17, 2007 at 07:36:21AM +0900, Corey Konrad 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? I'm just guessing here, but . . . It looks like you're expecting something akin to class behavior from a method. Perhaps what you want to do is something more like this: class Printer def initialize(arg) @arg = arg end def iterator_thingie yield @arg end end printer = Printer.new("ball") printer.iterator_thingie {|word| puts word} Then again, maybe that's not the kind of behavior you want. -- CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ] print substr("Just another Perl hacker", 0, -2);