From: Kyle Date: 2009-11-16T02:32:01+09:00 Subject: Re: Traversing the contents of a proc (for a DSL) --Boundary-00=_NsDALCwwTbMK7xC Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Generally - no. A proc is compiled code, it doesn't remember its own source code. There are libraries that provide some deeper introspection tools -- like the ParseTree / Ruby2Ruby libraries. Also, there are several DSLs very simllar to what you're talking about already -- check out Markaby: http://markaby.rubyforge.org/ On Sunday November 15 2009 3:58:08 am Siemen Baader wrote: > Hi List, > > Is it possible to traverse and modify the statements inside a Proc > after it has been created? > > I'm playing with a small DSL for HTML, which should look like this. > > html { > head { title {"Hello World"} } > body { > h1 { "hello"} > p { > "text body" > "more text" > } > } > } > > If it is possible to recursively traverse through the statements in > each block, I could join the lines together with the + operator, and > the implementation would be fairly easy - just recursive calls that > return text. But it does not look like this is possible... > > An alternative would be to create a new object for each node in the > HTML (eg body or h1), and let each method call inside this node assign > its content to an array there. Less functional style and more > overhead, but doable. But this requires either that I catch the > creation of new strings (no idea if that is possible) for them to be > added to the node as well, or that I introduce a new method to create > strings. > > Any comments highly appreciated! > > cheers, > Siemen --Boundary-00=_NsDALCwwTbMK7xC--