Wild and Crazy Metaprogramming with Camping

by Geoffrey Grosenbach

I try to expand my knowledge of Ruby by reading code from other developers I admire. In the past few weeks, that has meant reading the meager source of Camping, Why the Lucky Stiff’s tiny website framework.


It also helped to learn by writing a test framework for Camping. It works pretty well so far and has taught me a lot about Ruby, testing, and Camping. I hope to package it as a gem in the next few weeks.


Last night at the Seattle.rb, Evan Webb answered a few questions about the finer points of Ruby and metaprogramming. (Note: This is not a Camping tutorial.)


3 Comments

Dominic Mitchell
2006-06-14 14:23:54
In Perl, you've got the DATA filehandle, that you can (ab)use to read in the contents of the source file that's currently executing. You can probably do the same thing with it...
Amr Malik
2006-06-14 18:31:14
Continuing the Camping discussion, I'm wondering why method_missing is used again and again in related modules (like Markaby) to implement the HTML etc. entities repeatedly.


From a neophyte's perspective, I can understand this happening the very first time, but to call method missing a hundred times if the B tag appears a hundred times seems a violation of the DRY principle.
(Is my understanding correct?)


Is there a way to keep the dynamic nature of Markaby/Builder/Camping, but reduce the subsequent calls to method_missing ??


As others have noted elsewhere (Atrus for example) Markaby seems to take a sizable portion of the time making the method_missing calls..


I think the dynamic nature of Camping is brilliant, but I'm just wondering if there are ways to optimize the bits which seem to be resource/time hogs?

riffraff
2006-06-20 02:33:50
the DATA filehandle is available in ruby too, just stick __END__ in your file, but I don't think Geoffrey was thinking of that..


Anyway, I for one found the gsub+eval a dirty hack but quite.. _whyish..
Subclassing a runtime generated class on the other hand is quite useful, especially if you think of the "standard" class builders like Struct, DelegateClass and the likes.