From: Phil Tomson Date: 2006-08-29T03:53:17+09:00 Subject: Re: Need - macros feature On 8/28/06, Logan Capaldo wrote: > > On Aug 28, 2006, at 1:11 PM, Henry Savr wrote: > > > Matthew Johnson wrote: > >>> Thank you, guys > >>> Although it's not what I wanted, I have a lot of ideas in some > >>> directions. It's good for new in Ruby. > >>> Henry > >> > >> Can you describe some of your ideas? That would certainly help > >> foster discussion... > >> > >> Matthew > > > > There is nothiing new. It's as old as Univac > > What I want is a classic Macroprocessor tool. > > I mean: > > I keep pieces of souce code in some place. > > They may be included in source code as is. More interesting, you can > > adjust macroprocessor's output (hence, the input for Ruby interpeter) > > using paremters. > > > > 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. > > > > > > > > Ruby style allows to expect, that it may be done on-fly. > > > > So, finally the Ruby interpreter will execute the macroprocessor's > > output, not input. Besides the macroprocessor's input and output are > > saved, so you can return to them and change the simple lines of > > get/set-code to something more complicated and valueable. > > > > Again the get/set operations are just an EXAMPLE, used because > > everyone > > is familiar with this piece of code. Sure, using attributes is kind of > > solution for this particular case. (BTW. Thank you, Farrel Lifson, you > > post convinced me to learn more about attributes on this very > > beginning > > stage, and that was very usefull). I want the macro power options > > themselves. > > > I think maybe you underestimate what can be done with meta > programming in ruby that (almost) obviates the need for macros. > > class Module > # "macro" definition > def var(symbol) > define_method("#{symbol}=") { |value| instance_variable_set("@# > {symbol}", value) } > end > end > > class A > var :my_lovely_var > end > a = A.new > a.my_lovely_var = 1 > > Maybe if you can come up with a less trivial example, you can stump > us, but I doubt it. > While Ruby's metaprogramming facilities are indeed powerful, they still don't allow you to change the syntax of the language as Lisp macros allow. So, for example, if I wanted to define a new operator := for assignment semantics I can't do that with metaprogramming, but if Ruby had Lisp-like macros it would be possible. Phil