これを読んで何となく思いついた。
ERBのメソッドを書き換えればどうとでもできるなあ。やりたくないけど。
require 'erb' class ERB class Compiler # :nodoc: def compile(s) out = Buffer.new(self) content = '' scanner = make_scanner(s) scanner.scan do |token| next if token.nil? next if token == '' if scanner.stag.nil? case token when PercentLine out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 content = '' out.push(token.to_s) out.cr when :cr out.cr when '<%', '<%=', '<%#' scanner.stag = token out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 content = '' when "\n" content << "\n" out.push("#{@put_cmd} #{content_dump(content)}") content = '' when '<%%' content << '<%' else content << token end else case token when '%>' case scanner.stag when '<%' if content[-1] == ?\n content.chop! out.push(content) out.cr else out.push(content) end when '<%=' out.push("#{@insert_cmd}(::ERB::Util.h(#{content}).to_s)") when '<%#' # out.push("# #{content_dump(content)}") end scanner.stag = nil content = '' when '%%>' content << '%>' else content << token end end end out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0 out.close out.script end end end class Foo SCRIPT = <<EOS <h1><%= @name %></h1> <ul> <% ary.each do |x|%> <li><%= x %></li> <% end %> </ul> EOS def initialize(name) @name = name @erb = ERB.new(SCRIPT) end def foo(ary) @erb.result(binding) end end it = Foo.new('foo') puts it.foo([1,2,'<dia>'])
~/work$ ruby foo.rb
<h1>foo</h1>
<ul><li>1</li>
<li>2</li>
<li><dia></li>
</ul>