views:

73

answers:

1

I am using ERB for metaprogramming of some math language. If I could extend ERB functionality to handle %= tags, it would allow me to simplify my sources significantly. I simply want to get output of the line in analogy with <%= %>. I have tried to dig into /usr/lib/ruby/1.9.1/erb.rb file, but got lost very quickly. May be you can help with this problem?

A: 

Well, it seems I have managed it by myself. If you save the code at http://pastie.org/1056824 (or http://gist.github.com/487297) as extended_erb.rb and then call it in your script...

require 'extended_erb'
puts ERB.new(File.read('mytemplate.erb'), 0, '%').result

or run ERB from command line...

erb -r extended_erb  mytemplate.erb

then the following template...

<%= 1 %>
%= 2

will produce output desired

1
2
Andrey