[Newbies] Ruby macros

Bert Freudenberg bert at impara.de
Sun Jul 2 11:20:44 UTC 2006

Am 02.07.2006 um 06:33 schrieb itsme213:

> I miss my Ruby macros (1st day in Squeak and I love it ... dislike  
> the camelCase tho').

To add a variable to a class, use

	someClass addInstVarName: 'myInstVar'.

To add an accessor with initialization code, compile one:

	someClass compile: 'myInstVar ^myInstVar ifNil: [myInstVar :=  
OrderedCollection new]'.

Similarly, use #removeInstVarName: and  #removeSelector: to remove  
what you added.

To automate the process, you could implement #noteCompilationOf:meta:  
to add/remove stuff automatically when some "spec" method was changed.

HOWEVER, such "magic" behavior is not considered good practice in  
Smalltalk. We do use code generation for repetitive tasks, but that  
is usually triggered not automagically, but invoked manually - we put  
the actual call in a comment that you can just double-click and  
execute. An example for this is the various "#fields" methods for  
ExternalStructure subclasses which include a "self defineFields"  
comment that should be executed when you changed the field definitions.

The reason for this non-magic is mostly because you're dealing with  
an evolving, live system that is not bootstrapped every time you run  
your app like in Ruby. After you let the system define the methods,  
you're free to change them in a browser to suit your needs. In  
Smalltalk, you're actually modifying the class, not a textual blue- 
print that is executed later. That's a huge difference, it's hard to  
grasp when you come from a different environment, admittedly, but you  
need to get your head around it to become a proficient Smalltalker.  
Once you got enlightened, you'll wonder how you could ever have  
longed for editing a Smalltalk class in a text file ;-)

- Bert -

More information about the Beginners mailing list