# Delayed string interpolation

**URL:** <https://rubytalk.org/t/delayed-string-interpolation/16307>\
**Category:** ruby-talk\
**Created:** [7 February 2005 05:24 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307 "2005-02-07T05:24:18Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 05:24 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/1 "2005-02-07T05:24:18Z")

</div>

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()  
&nbsp;&nbsp;&nbsp;&nbsp;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.

Also, where is the "\<\<MARKER" syntax (nothing in ri?) and Ruby string  
interpolation officially documented, if anywhere?

Thanks,  
Navin.

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 05:36 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/2 "2005-02-07T05:36:17Z")

</div>

> 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.

To add to that, I thought about leaving a few "%s" in the string and  
replacing them later, but that too seems ugly because I have feed in  
variables in order and sometimes even repeatedly.

Thanks,  
Navin.

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [7 February 2005 06:23 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/3 "2005-02-07T06:23:41Z")

</div>

> I'm generating an HTML header as a String that makes heavy use of  
> interpolation. Essentially I have something like:
> 
> def make\_html\_header()  
> &nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;def reeval(b = TOPLEVEL\_BINDING)  
&nbsp;&nbsp;&nbsp;&nbsp;eval(%Q{\<\<"END\_REEVAL"\n} + self + "\nEND\_REEVAL\n", b)  
&nbsp;&nbsp;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](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

> **···**
>
> From: "Navindra Umanee" \<navindra@cs.mcgill.ca\>

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [7 February 2005 09:05 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/4 "2005-02-07T09:05:09Z")

</div>

"Navindra Umanee" \<navindra@cs.mcgill.ca\> 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()  
> &nbsp;&nbsp;&nbsp;&nbsp;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.

One option is two levels of interpolation:

generator = "my gen"  
application = "my test app"  
header = "\"\<html\>.....#{generator} .... #{application} .... \#{title}\""  
....  
title = "foo"  
real\_header = eval header

Or you create a lambda for that:

def create\_header(generator, application)  
&nbsp;&nbsp;eval "lambda {|title| \"\<html\>.....#{generator} .... #{application} ....  
\#{title}\" }"  
end

> > x = create\_header generator, application

=\> #\<Proc:0x100c2990@(eval):1\>

> > x.call "foo"

=\> "\<html\>.....my gen .... my test app .... foo"

> Also, where is the "\<\<MARKER" syntax (nothing in ri?) and Ruby string  
> interpolation officially documented, if anywhere?

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut\_stdtypes.html#S2](http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_stdtypes.html#S2)

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 16:05 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/5 "2005-02-07T16:05:53Z")

</div>

Hi,

Thanks a lot for both eval solutions (exactly what I was looking for)  
and the template suggestion.

I still have nightmares about Zope's DTML, so I wasn't sure I wanted  
to go with templating this time. I think as I encounter more and more  
issues however I might have to consider to a templating solution  
afterall.

Of the two templating solutions suggested so far, Amrita seems too  
indirect for my tastes. I'd rather directly code what my HTML looks  
like than have it be the result of some fancy/unknown  
computation... Not to mention that's just going to come back and bite  
me when the implementation changes (or doesn't change).

As for ERB, I'm not yet sure how it's better or easier than the simple  
2-level string eval I wanted to do. It seems pretty much the same  
except with different enclosing tags and more overhead. ERB::Util has  
some interesting subroutines though. I'll have to look more closely  
to understand the advantages.

Thanks,  
Navin.

---

<div class="post-metadata">

**Author:** ![Charles\_Mills1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Charles\_Mills1](https://rubytalk.org/u/Charles_Mills1)\
**Post date:** [7 February 2005 06:35 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/6 "2005-02-07T06:35:10Z")

</div>

Navindra Umanee wrote:

> \> 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.
> 
> To add to that, I thought about leaving a few "%s" in the string and  
> replacing them later, but that too seems ugly because I have feed in  
> variables in order and sometimes even repeatedly.

You could write your interpolater. Here is an example:

$ irb  
irb(main):001:0\> require 'tmp.rb'  
=\> true  
irb(main):002:0\> s = ":hey :day"  
=\> ":hey :day"  
irb(main):003:0\> s.interpolate(:hey =\> "hello", :day=\>"yo")  
=\> "hello yo"

$ cat tmp.rb  
class String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def interpolate(hash)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gsub(/:(\w+)/) { hash[$1.to\_sym] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def interpolate!(hash)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gsub!(/:(\w+)/) { hash[$1.to\_sym] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

$

-Charlie

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [7 February 2005 10:00 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/7 "2005-02-07T10:00:10Z")

</div>

By this point I'd have switched to a proper template engine, just to  
make sure the eval didn't bring in some hairy corner cases (am I being  
overly paranoid?) I was going to suggest this earlier, in fact, but I  
felt amrita was probably a bit too heavy for this case.

martin

> **···**
>
> Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > One option is two levels of interpolation:
> > 
> > generator = "my gen"  
> > application = "my test app"  
> > header = "\"\<html\>.....#{generator} .... #{application} .... \#{title}\""  
> > ...  
> > title = "foo"  
> > real\_header = eval header

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 16:52 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/8 "2005-02-07T16:52:01Z")

</div>

I have to say this is such a really neat hack... it says so much  
about Ruby too.

I had a hard time understanding it until I realised that the first  
END\_REEVAL doesn't need to be in quotes. Writing it as:

&nbsp;&nbsp;&nbsp;&nbsp;eval("\<\<END\_REEVAL\n" + self + "\nEND\_REEVAL\n", b)

seems to work just as well. Any reason you did it that way?

Cheers,  
Navin.

> **···**
>
> Bill Kelly \<billk@cts.com\> wrote:
> 
> > I dunno if this counts as elegant or not, 🙂  
> > but a couple years ago, I used:
> > 
> > class String  
> > &nbsp;&nbsp;def reeval(b = TOPLEVEL\_BINDING)  
> > &nbsp;&nbsp;&nbsp;&nbsp;eval(%Q{\<\<"END\_REEVAL"\n} + self + "\nEND\_REEVAL\n", b)  
> > &nbsp;&nbsp;end  
> > end

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [8 February 2005 13:59 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/9 "2005-02-08T13:59:27Z")

</div>

Sorry, I missed this message when you posted it.

I was thinking you would Heredoc a erb template using normal #{ ... } interpolation for the things you wanted to calculate up front and erb \<%= ... %\> place holders for the bits to fill in later. Then you could just generate the template repeatedly as needed.

Hope that clears up my idea, though I'm sure you have a solution by now.

James Edward Gray II

> **···**
>
> On Feb 7, 2005, at 10:05 AM, Navindra Umanee wrote:
> 
> > As for ERB, I'm not yet sure how it's better or easier than the simple  
> > 2-level string eval I wanted to do. It seems pretty much the same  
> > except with different enclosing tags and more overhead. ERB::Util has  
> > some interesting subroutines though. I'll have to look more closely  
> > to understand the advantages.

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [7 February 2005 14:11 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/10 "2005-02-07T14:11:46Z")

</div>

This was my first thought when reading the original message as well. Might want to have a look at erb in the standard library.

James Edward Gray II

> **···**
>
> On Feb 7, 2005, at 4:00 AM, Martin DeMello wrote:
> 
> > Robert Klemme \<bob.news@gmx.net\> wrote:
> > 
> > > One option is two levels of interpolation:
> > > 
> > > generator = "my gen"  
> > > application = "my test app"  
> > > header = "\"\<html\>.....#{generator} .... #{application} .... \#{title}\""  
> > > ...  
> > > title = "foo"  
> > > real\_header = eval header
> > 
> > By this point I'd have switched to a proper template engine, just to  
> > make sure the eval didn't bring in some hairy corner cases (am I being  
> > overly paranoid?) I was going to suggest this earlier, in fact, but I  
> > felt amrita was probably a bit too heavy for this case.

---

<div class="post-metadata">

**Author:** ![Bill\_Kelly](https://avatars.discourse-cdn.com/v4/letter/b/cc9497/32.png) [@Bill\_Kelly](https://rubytalk.org/u/Bill_Kelly)\
**Post date:** [7 February 2005 17:23 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/11 "2005-02-07T17:23:33Z")

</div>

> \> 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
> 
> I have to say this is such a really neat hack... it says so much  
> about Ruby too.

Thanks! And, indeed: thanks Matz !! =D

> I had a hard time understanding it until I realised that the first  
> END\_REEVAL doesn't need to be in quotes. Writing it as:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;eval("\<\<END\_REEVAL\n" + self + "\nEND\_REEVAL\n", b)
> 
> seems to work just as well. Any reason you did it that way?

I'm guessing I got into the habit early on to remind  
myself that ruby lets you specify single-quote semantics  
if you want them, like:

x = \<\<'ENDTEXT'  
now things like \n are literal  
as they would be in 'single quote' strings  
ENDTEXT

So I probably used \<\<"END\_REEVAL" trying to be specific  
about my request for double-quote semantics.... However  
it seems to have just become a habit. 🙂 Maybe I'll  
stop doing it... Hehe..

Regards,

Bill

> **···**
>
> From: "Navindra Umanee" \<navindra@cs.mcgill.ca\>
> 
> > Bill Kelly \<billk@cts.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 18:08 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/12 "2005-02-07T18:08:12Z")

</div>

Oh, hmph. I didn't even know that. It's not really documented is it?

I didn't even realise \n would be converted to a newline in heredoc as  
well. Going to have to be really really careful about things like  
user-input, I guess.

Thanks for the useful information. 🙂

Cheers,  
Navin.

> **···**
>
> Bill Kelly \<billk@cts.com\> wrote:
> 
> > I'm guessing I got into the habit early on to remind  
> > myself that ruby lets you specify single-quote semantics  
> > if you want them, like:
> > 
> > x = \<\<'ENDTEXT'  
> > now things like \n are literal  
> > as they would be in 'single quote' strings  
> > ENDTEXT

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [7 February 2005 18:21 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/13 "2005-02-07T18:21:36Z")

</div>

Navindra Umanee wrote:

> > I'm guessing I got into the habit early on to remind  
> > myself that ruby lets you specify single-quote semantics  
> > if you want them, like:
> > 
> > x = \<\<'ENDTEXT'  
> > now things like \n are literal  
> > as they would be in 'single quote' strings  
> > ENDTEXT
> 
> Oh, hmph. I didn't even know that. It's not really documented is it?
> 
> I didn't even realise \n would be converted to a newline in heredoc as  
> well. Going to have to be really really careful about things like  
> user-input, I guess.
> 
> Thanks for the useful information. 🙂

\n will only be converted it you allow string expansion,

#a bunch of blank lines  
x = \<\<"ENDTEXT"  
\n\n\n\n\n  
ENDTEXT

#a bunch of literal \n 's  
x = \<\<'ENDTEXT'  
\n\n\n\n\n  
ENDTEXT

Note the double-quotes compared to the single quotes.

Zach

> **···**
>
> > Bill Kelly \<billk@cts.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Navindra\_Umanee](https://avatars.discourse-cdn.com/v4/letter/n/d78d45/32.png) [@Navindra\_Umanee](https://rubytalk.org/u/Navindra_Umanee)\
**Post date:** [7 February 2005 18:50 UTC](https://rubytalk.org/t/delayed-string-interpolation/16307/14 "2005-02-07T18:50:37Z")

</div>

Well, I'm using heredoc because I don't want to specify \n and the  
such. The string is specified and looks almost exactly as the document.

But I \*do\* want #{} expansion. This is the way it pretty much works  
on bash heredoc by default.

Cheers,  
Navin.

> **···**
>
> Zach Dennis \<zdennis@mktec.com\> wrote:
> 
> > #a bunch of blank lines  
> > x = \<\<"ENDTEXT"  
> > \n\n\n\n\n  
> > ENDTEXT
> > 
> > #a bunch of literal \n 's  
> > x = \<\<'ENDTEXT'  
> > \n\n\n\n\n  
> > ENDTEXT
