On Aug 18, 2005, at 8:47 AM, Gavin Kistner wrote:
> How would I patch ERB so that puts and print were aliased to  
> concatenate onto the current eoutvar? This aliasing should be setup  
> at the beginning of #result, and removed at the end of #result.

Here's my horrific hack that does it. It's particularly hacky because  
it requires that the 'eoutvar' be set to have a @ at the beginning of  
the name (an instance variable). The real solution I suppose requires  
a change to the ERB::Compiler#compile method, to replace calls to  
puts and print with the local variable concatenation inline. But that  
was beyond my foo.

#in the ERB class
     def initialize(str, safe_level=nil, trim_mode=nil,  
eoutvar='_erbout')
         @safe_level = safe_level
         compiler = ERB::Compiler.new(trim_mode)
         set_eoutvar(compiler, eoutvar)
         @src = <<-ENDIOBORK
             module Kernel
                 alias_method :_oldputs, :puts
                 alias_method :_oldprint, :print
                 def puts(s)
                     #{eoutvar}<<s+"\\n"
                 end
                 def print(s)
                     #{eoutvar}<<s
                 end
             end
         ENDIOBORK
         @src << compiler.compile(str)
         @src << "\n" + <<-FIXIOBORK
             module Kernel
                 alias_method :puts, :_oldputs
                 alias_method :print, :_oldprint
             end
             #{eoutvar}
         FIXIOBORK
         @filename = nil
     end