From: "Navindra Umanee" <navindra / cs.mcgill.ca>
>
> I'm generating an HTML header as a String that makes heavy use of
> interpolation.  Essentially I have something like:
> 
> def make_html_header()
>     str <<END
> blah #{blah} blah #{blah} blah #{blah} 
> END
> end
> 
> This works great.  Most of the interpolations are essentially
> configuration parameters that I've elegantly stuffed into the instance
> variables of a Singleton class, some are method invocations.  These
> parameters don't change often, if at all, and the header is more or
> less identical for each HTML page that is generated.
> 
> So I would like to generate the header once and stuff it in an
> instance variable.  However, there are one or two variable
> interpolations that are unique to each page e.g. the title of the
> page.
> 
> How can I specify that some interpolations should be interpolated
> immediately whilst a few others should be delayed until the next time
> the String is evaluated?  I can think of some hacky ways i.e. doing
> subsequent string processing and substitions, I'm just wondering if
> there is a more elegant way.

I dunno if this counts as elegant or not, :)
but a couple years ago, I used:

class String
  def reeval(b = TOPLEVEL_BINDING)
    eval(%Q{<<"END_REEVAL"\n} + self + "\nEND_REEVAL\n", b)
  end
end

in similar circumstances.  I would just put a backslash
in front of any interpolations I wanted to defer.  Then
I'd call reeval at some later point at run-time.  I would
also pass in a binding, so that my (deferred) interpolations
would have access to instance methods of the class performing
the reeval.

I'd described it in more detail in:
http://ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/59979
(the external links in that post no longer work, sorry.)


HTH,

Regards,

Bill