# String interpolation method?

**URL:** <https://rubytalk.org/t/string-interpolation-method/44819>\
**Category:** ruby-talk\
**Created:** [27 February 2008 22:53 UTC](https://rubytalk.org/t/string-interpolation-method/44819 "2008-02-27T22:53:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [27 February 2008 22:53 UTC](https://rubytalk.org/t/string-interpolation-method/44819/1 "2008-02-27T22:53:36Z")

</div>

Ok, I'm being stupid probably...

But I can't spot a method to do string interpolation.

I have a string...

a = 'bra#{c}ket'

The variable c isn't available at the stage of setting that up. Hence  
the use of '' instead of "".

The variable will be available later....

At that stage I want to do something like...

c=' see '  
a.interpolate

The closest I can get is a bit fugly...

eval "\"#{a}\""  
=\> "bra see ket"

Any better way?

Thanks,

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : [john.carter@tait.co.nz](mailto:john.carter@tait.co.nz)  
New Zealand

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [27 February 2008 23:06 UTC](https://rubytalk.org/t/string-interpolation-method/44819/2 "2008-02-27T23:06:50Z")

</div>

John Carter wrote:

> Ok, I'm being stupid probably...
> 
> But I can't spot a method to do string interpolation.
> 
> I have a string...
> 
> a = 'bra#{c}ket'
> 
> The variable c isn't available at the stage of setting that up. Hence  
> the use of '' instead of "".
> 
> The variable will be available later....
> 
> At that stage I want to do something like...
> 
> c=' see '  
> a.interpolate
> 
> The closest I can get is a bit fugly...
> 
> eval "\"#{a}\""  
> =\> "bra see ket"
> 
> Any better way?

eval a

Best regards,

Jari Williamsson

---

<div class="post-metadata">

**Author:** ![Mark\_Bush](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@Mark\_Bush](https://rubytalk.org/u/Mark_Bush)\
**Post date:** [27 February 2008 23:24 UTC](https://rubytalk.org/t/string-interpolation-method/44819/3 "2008-02-27T23:24:03Z")

</div>

John Carter wrote:

> eval "\"#{a}\""  
> =\> "bra see ket"
> 
> Any better way?

Not better. Just different:

eval '"%s"'%(a)

Does the same thing but without needing the backslashes.

I don't see any other way really. You must have a double eval in some  
manner. One to get the value of a and one to evaluate the result. The  
first eval can be explicit (as in your example with #{}) or implicit (as  
in my example).

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<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:** [27 February 2008 23:31 UTC](https://rubytalk.org/t/string-interpolation-method/44819/4 "2008-02-27T23:31:47Z")

</div>

You want a templating solution, like the ERb code that ships with Ruby:

&nbsp;&nbsp;&nbsp;#!/usr/bin/env ruby -wKU

&nbsp;&nbsp;&nbsp;require "erb"

&nbsp;&nbsp;&nbsp;template = ERB.new("bra\<%= c %\>ket")

&nbsp;&nbsp;&nbsp;# and later...  
&nbsp;&nbsp;&nbsp;c = "c"  
&nbsp;&nbsp;&nbsp;p template.result

&nbsp;&nbsp;&nbsp;\_\_END\_\_

Hope that helps.

James Edward Gray II

> **···**
>
> On Feb 27, 2008, at 4:53 PM, John Carter wrote:
> 
> > Ok, I'm being stupid probably...
> > 
> > But I can't spot a method to do string interpolation.
> > 
> > I have a string...
> > 
> > a = 'bra#{c}ket'
> > 
> > The variable c isn't available at the stage of setting that up.

---

<div class="post-metadata">

**Author:** ![yermej](https://avatars.discourse-cdn.com/v4/letter/y/b2d939/32.png) [@yermej](https://rubytalk.org/u/yermej)\
**Post date:** [27 February 2008 23:50 UTC](https://rubytalk.org/t/string-interpolation-method/44819/5 "2008-02-27T23:50:04Z")

</div>

I would do it thusly:

a = "bra%sket"  
c = ' see '  
a % c  
=\> "bra see ket"

> **···**
>
> On Feb 27, 4:53 pm, John Carter \<john.car...@tait.co.nz\> wrote:
> 
> > Ok, I'm being stupid probably...
> > 
> > But I can't spot a method to do string interpolation.
> > 
> > I have a string...
> > 
> > a = 'bra#{c}ket'
> > 
> > The variable c isn't available at the stage of setting that up. Hence  
> > the use of '' instead of "".
> > 
> > The variable will be available later....
> > 
> > At that stage I want to do something like...
> > 
> > c=' see '  
> > a.interpolate
> > 
> > The closest I can get is a bit fugly...
> > 
> > eval "\"#{a}\""  
> > =\> "bra see ket"
> > 
> > Any better way?
> > 
> > Thanks,
> > 
> > John Carter Phone : (64)(3) 358 6639  
> > Tait Electronics Fax : (64)(3) 359 4632  
> > PO Box 1645 Christchurch Email : john.car...@tait.co.nz  
> > New Zealand

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [28 February 2008 02:39 UTC](https://rubytalk.org/t/string-interpolation-method/44819/6 "2008-02-28T02:39:43Z")

</div>

=\> require 'facets/string/interpolate'  
=\> true

> > a = 'bra#{c}ket'

=\> "bra\#{c}ket"

> > c = 4

=\> 4

> > String.interpolate{a}

=\> "bra4ket"

T.

---

<div class="post-metadata">

**Author:** ![Jari\_Williamsson](https://avatars.discourse-cdn.com/v4/letter/j/c4cdca/32.png) [@Jari\_Williamsson](https://rubytalk.org/u/Jari_Williamsson)\
**Post date:** [27 February 2008 23:12 UTC](https://rubytalk.org/t/string-interpolation-method/44819/7 "2008-02-27T23:12:31Z")

</div>

Jari Williamsson wrote:

> John Carter wrote:
> 
> > Ok, I'm being stupid probably...
> > 
> > But I can't spot a method to do string interpolation.
> > 
> > I have a string...
> > 
> > a = 'bra#{c}ket'
> > 
> > The variable c isn't available at the stage of setting that up. Hence  
> > the use of '' instead of "".
> > 
> > The variable will be available later....
> > 
> > At that stage I want to do something like...
> > 
> > c=' see '  
> > a.interpolate
> > 
> > The closest I can get is a bit fugly...
> > 
> > eval "\"#{a}\""  
> > =\> "bra see ket"
> > 
> > Any better way?
> 
> eval a

I spoke too soon - my suggestion will not work...

Best regards,

Jari Williamsson
