# ERB as a macro pre-processor?

**URL:** <https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744>\
**Category:** Ruby\
**Created:** [October 11, 2006, 7:41am UTC](https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744 "2006-10-11T07:41:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [October 11, 2006, 7:41am UTC](https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744/1 "2006-10-11T07:41:49Z")

</div>

“The Ruby Way” (pp. 43-45) shows a couple of different ways  
to generate methods dynamically (using eval). Looking at the  
examples, I began to wonder about different Ways To Do It.

One possibility that occurred to me is to use ERB as a macro  
pre-processor. The book suggests the following code:

if platform == Windows  
def my\_action  
action1  
end  
if platform == Linux  
def my\_action  
action2  
end  
else  
def my\_action  
default\_action  
end  
end

Although this is approach is direct and simple, it seems like it  
wouldn’t scale very well, given multiple variations. However, I  
think that the following approach might:

% cat erb\_ml  
#!/usr/bin/env ruby

require ‘erb’

def action\_D; print “default action\n”; end  
def action\_L; print “Linux action\n”; end  
def action\_W; print “Windows action\n”; end

action = { :Linux =\> ‘action\_L’,  
:Windows =\> ‘action\_W’ }

template = ERB.new \<\<-EOF  
def my\_action  
\<%= action[platform] || ‘action\_D’ %\>  
end  
EOF

for platform in [:Linux, :Windows, :Plan9] do  
eval template.result(binding)  
print "#{platform.to\_s}: "  
my\_action  
end

% erb\_ml  
Linux: Linux action  
Windows: Windows action  
Plan9: default action

Obviously, the “action” hash could be replaced by other  
code-generation and/or -retrieval methods. Is anyone  
here using ERB in this manner? Are there any caveats  
or alternative approaches that I should be aware of?

## -r

[http://www.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [October 11, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744/2 "2006-10-11T16:05:18Z")

</div>

On Wed, Oct 11, 2006 at 02:40:58PM +0900, Rich M. wrote:

> ```
> end
> 
> ```
> 
> Although this is approach is direct and simple, it seems like it  
> def action\_L; print “Linux action\n”; end
> 
> Obviously, the “action” hash could be replaced by other  
> code-generation and/or -retrieval methods. Is anyone  
> here using ERB in this manner? Are there any caveats  
> or alternative approaches that I should be aware of?  
> Bah I say! How does that saying go, “Replace conditionals with  
> polymorphism”?

class Platform  
end

class Linux \< Platform  
def define\_methods  
def a  
puts “linux”  
end  
end  
end

class Windows \< Platform  
def define\_methods  
def a  
puts “windows”  
end  
end  
end

class Platform  
def self.get\_platform  
case RUBY\_PLATFORM  
when /linux/  
Linux  
when /win32/  
Windows  
end  
end  
end

platform = Platform.get\_platform.new  
platform.define\_methods

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [October 11, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744/3 "2006-10-11T16:37:42Z")

</div>

On Wed, 11 Oct 2006, Rich M. wrote:

> “The Ruby Way” (pp. 43-45) shows a couple of different ways  
> to generate methods dynamically (using eval). Looking at the  
> examples, I began to wonder about different Ways To Do It.  
>   
> Obviously, the “action” hash could be replaced by other  
> code-generation and/or -retrieval methods. Is anyone  
> here using ERB in this manner? Are there any caveats  
> or alternative approaches that I should be aware of?

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/4e2f0f29308c9647/d985a8bf050d88d5?lnk=gst&q=r4&rnum=1#d985a8bf050d88d5](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/4e2f0f29308c9647/d985a8bf050d88d5?lnk=gst&q=r4&rnum=1#d985a8bf050d88d5)

i did quite a bit more work on it but never released…

cheers.

-a

---

<div class="post-metadata">

**Author:** ![iceburgh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c4cdca/32.png) [@iceburgh](https://www.ruby-forum.com/u/iceburgh)\
**Post date:** [October 11, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/erb-as-a-macro-pre-processor/71744/4 "2006-10-11T17:54:29Z")

</div>

At 11:36 PM +0900 10/11/06, removed\_email\_address@domain.invalid wrote:

> i did quite a bit more work on it but never released…

interesting stuff; why not put out a WIP snapshot?

Both r4 and ERB allow the use of arbitrary Ruby code. It  
would be interesting to see which features of each work  
better for what kinds of problems.

## -r

[http://www.cfcl.com/rdm](http://www.cfcl.com/rdm) Rich M.  
[http://www.cfcl.com/rdm/resume](http://www.cfcl.com/rdm/resume) removed\_email\_address@domain.invalid  
[http://www.cfcl.com/rdm/weblog](http://www.cfcl.com/rdm/weblog) +1 650-873-7841

Technical editing and writing, programming, and web development
