# String interpolation

**URL:** <https://www.ruby-forum.com/t/string-interpolation/113022>\
**Category:** Ruby\
**Created:** [August 26, 2007, 11:21am UTC](https://www.ruby-forum.com/t/string-interpolation/113022 "2007-08-26T11:21:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [August 26, 2007, 11:21am UTC](https://www.ruby-forum.com/t/string-interpolation/113022/1 "2007-08-26T11:21:11Z")

</div>

a = 5  
b = “#{a}”  
puts b

a = 6  
puts b

Returns:  
5  
5

which is clear to me, why. But is there a way to define such a string  
and  
interpolate it at a later time?

Thomas

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [August 26, 2007, 11:38am UTC](https://www.ruby-forum.com/t/string-interpolation/113022/2 "2007-08-26T11:38:40Z")

</div>

Thomas W. wrote:

> a = 5  
> b = “#{a}”  
> puts b
> 
> a = 6  
> puts b
> 
> Returns:  
> 5  
> 5
> 
> which is clear to me, why. But is there a way to define such a string  
> and  
> interpolate it at a later time?
> 
> Thomas

a = 5  
b = proc { “a is now: #{a}” }  
def b.to\_s; call; end  
puts b # !\> a is now: 5  
a = 6  
puts b # !\> a is now: 6

Enjoy

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [August 26, 2007, 11:46am UTC](https://www.ruby-forum.com/t/string-interpolation/113022/3 "2007-08-26T11:46:14Z")

</div>

On Sun, 26 Aug 2007 18:38:46 +0900, Stefan R. wrote:

> a = 5  
> b = proc { “a is now: #{a}” }  
> def b.to\_s; call; end  
> puts b # !\> a is now: 5  
> a = 6  
> puts b # !\> a is now: 6

Many thanks for the quick help !!!  
Thomas

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [August 26, 2007, 11:49am UTC](https://www.ruby-forum.com/t/string-interpolation/113022/4 "2007-08-26T11:49:14Z")

</div>

On Aug 26, 2007, at 11:20 AM, Thomas W. wrote:

> which is clear to me, why. But is there a way to define such a  
> string and  
> interpolate it at a later time?

You normally use a templating system, for example:

require ‘erb’

b = ERB.new(“a is \<%= a %\>”)

a = 5  
puts b.result(binding) # -\> a is 5

a = 6  
puts b.result(binding) # -\> a is 6

– fxn

---

<div class="post-metadata">

**Author:** ![wet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/848f3c/32.png) [@wet](https://www.ruby-forum.com/u/wet)\
**Post date:** [August 26, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/string-interpolation/113022/5 "2007-08-26T17:14:01Z")

</div>

Thomas W. wrote:

> which is clear to me, why. But is there a way to define such a string and  
> interpolate it at a later time?

You are asking how to do a “block closure”. Study that, because it’s a  
major  
Ruby topic and a very good design technique. I have not yet found a way  
to  
over-use or abuse blocks in Ruby!

A ‘lambda’ is one of the block systems that can bond with the variables  
around it. So stick your string evaluator into a lambda, and call it:

> > a = 5  
> > =\> 5
> 
> > q = lambda{"#{a}"}  
> > =\> #Proc:0xb721d4d4@:7(irb)
> 
> > q.call  
> > =\> “5”
> 
> > a = 6  
> > =\> 6
> 
> > q.call  
> > =\> “6”

Block closures are a very good design technique because a has a very  
limited  
scope over a very long lifespan. We could have stored that q and used it  
later. So a becomes very encapsulated.
