From: Bob Hutchison Date: 2005-09-11T22:58:11+09:00 Subject: Re: r4 - the simplest ruby pre-processor Hi, With a tiny change to this you can also use ruby's embedded value substitution (interpolation), like: | | some_number = 42 | Hello there! some number: [[[#{some_number}]]] to get output like: Hello there! some number: [[[42]]] That one change is from: > "puts #{line.chomp.dump}\n" > to: "puts \"#{line.chomp}\"\n" (just added the quotes). This is a very interesting little toy. Maybe not a toy at all. Cheers, Bob On Sep 11, 2005, at 7:38 AM, Christian Neukirchen wrote: > "Ara.T.Howard" writes: > > > >> all this talk about pre-processors and erb got me thinking : what >> would it >> take to code up a ruby pre-procssors that could be used for any >> laguange. my >> first crack is 38 lines of ruby that pre-process any files on the >> command line >> (or stdin) using ruby as the macro language and a convenience >> function 'macro' >> which can be used to define ruby methods which return test that is >> used as >> output. functions declared this way need not do any explicit io. >> >> > > Alternative implementation ("r5"): > > def macro(name, &block) > Kernel.send(:define_method, name) { |*args| > puts block.call(args) > } > end > > send(($DEBUG ? "puts" : "eval"), ARGF.readlines.map { |line| > if line[0] == ?| > line[1..-1] > else > "puts #{line.chomp.dump}\n" > end > }.join) > > Happy hacking, > -- > Christian Neukirchen http:// > chneukirchen.org > > > ---- Bob Hutchison -- blogs at Recursive Design Inc. -- Raconteur --