From: dblack@... Date: 2002-12-28T00:06:41+09:00 Subject: Re: viral arguments Hi -- On Fri, 27 Dec 2002, Tom Sawyer wrote: > thanks for all the response on the float problem. alot of people jumped on > that one. guess everyone but me knew the answer. :-) > > anyway, i'm sitting here coding my billy-bad-ass cgi accounting system, in > yours truly, ruby, and i'm running into small sticky spots in which i want to > keep a library i created pure for general use, yet i run into particular > situations where the behavior needs a minor tweak to work. upon looking at > these recoccuring catches, i started to develop a notion of "virus-like" > arguments. > > the idea is that rather then pass simple argumments, one could pass a little > piece of code that would execute in the context of the method. > > here's a foo example (vproc is like proc but viral): > > def foo(x) > i = 1 > puts "#{x + i}" > end > > puts foo(4) --> 5 > puts foo(vproc {i + 1}) --> 3 > > any thoughts? can ruby achieve something like this already? It's not clear to me why executing "i+1" at the top of the method would do anything but raise an exception (unless the object has an #i method)... but in any case, see Kent's answer about blocks. Also, consider the impossibility of maintaining and debugging code where the names of a method's local variables have to be known outside the method. That's why they're local :-) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav