From: Robert Klemme Date: 2005-02-07T18:05:09+09:00 Subject: Re: delayed string interpolation "Navindra Umanee" schrieb im Newsbeitrag news:20050207002403.A12145@cs.mcgill.ca... > Hi, > > 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 < 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. One option is two levels of interpolation: generator = "my gen" application = "my test app" header = "\".....#{generator} .... #{application} .... \#{title}\"" .... title = "foo" real_header = eval header Or you create a lambda for that: def create_header(generator, application) eval "lambda {|title| \".....#{generator} .... #{application} .... \#{title}\" }" end >> x = create_header generator, application => # >> x.call "foo" => ".....my gen .... my test app .... foo" > Also, where is the "< interpolation officially documented, if anywhere? http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S2 Kind regards robert