From: Eric Mahurin Date: 2006-08-29T04:40:18+09:00 Subject: Re: Need - macros feature ------=_Part_95669_19303434.1156794013062 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline > > > E X A M P L E: > > > =============== > > > You wrote the macro once and put it in macro library > > > (here I "invented" the "macrolanguage" on-fly, while writing the > > > example. I hope, the "language" is clear) > > > > > > ====this is a macro %var with argument %%x definition============== > > > macro: %var (%%x) > > > > > > {def %%x=(value) > > > @%%x = value > > > return @%%x > > > end} > > > ======== end of macro %var definition ============================= > > > You may consider the macro definition as a template. > > > > > > > > > Now, somewhere in your code you write: > > > ... > > > %var(my_lovely_var) > > > ... > > > > > > Once found the %var, macroprocessor should make substitutions and > > > produce this piece: > > > > > > def my_lovely_var=(value) > > > @my_lovely_var = value > > > return @my_lovely_var > > > end > > > > > > which will be inserted into code on the place, where the statement > > > %var(my_lovely_var) was instead of it. Here is one way: var = lambda { |x| %q{ def #{x}=(value) #{x} = value return #{x} end }} eval(var["my_lovely_var"]) You can easily define and use multiple levels of macros. This is kind-of the way I do it in my parser generation to make a relatively flat parser. I add the ability to use objects inside the macro so that they have the same flexibility as closures. You have to explicitly pass the objects you want to use, though. Eric ------=_Part_95669_19303434.1156794013062--