# String interpolation method?

**URL:** <https://www.ruby-forum.com/t/string-interpolation-method/130403>\
**Category:** Ruby\
**Created:** [February 27, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403 "2008-02-27T23:54:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 27, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/1 "2008-02-27T23:54:53Z")

</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 C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 12:07am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/2 "2008-02-28T00:07:48Z")

</div>

John C. wrote:

> =\> “bra see ket”
> 
> Any better way?

eval a

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 12:13am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/3 "2008-02-28T00:13:06Z")

</div>

Jari W. wrote:

> > the use of ‘’ instead of “”.  
> > eval “"#{a}"”  
> > =\> “bra see ket”
> > 
> > Any better way?
> 
> eval a

I spoke too soon - my suggestion will not work…

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 12:24am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/4 "2008-02-28T00:24:11Z")

</div>

John C. 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).

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 12:52am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/5 "2008-02-28T00:52:27Z")

</div>

On Feb 27, 4:53 pm, John C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> “bra see ket”
> 
> Any better way?
> 
> Thanks,
> 
> John C. Phone : (64)(3) 358 6639  
> Tait Electronics Fax : (64)(3) 359 4632  
> PO Box 1645 Christchurch Email : [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> New Zealand

I would do it thusly:

a = “bra%sket”  
c = ’ see ’  
a % c  
=\> “bra see ket”

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 12:32am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/6 "2008-02-28T00:32:30Z")

</div>

On Feb 27, 2008, at 4:53 PM, John C. 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.

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

#!/usr/bin/env ruby -wKU

require “erb”

template = ERB.new(“bra\<%= c %\>ket”)

# and later…

c = “c”  
p template.result

**END**

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 3:40am UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/7 "2008-02-28T03:40:39Z")

</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:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 28, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/8 "2008-02-28T21:45:14Z")

</div>

On Thu, 28 Feb 2008, Trans wrote:

> =\> require ‘facets/string/interpolate’

Hmm…

Very neat in application…

> > > String.interpolate{a}

but…

```
 def interpolate(&str)
   eval "%{#{str.call}}", str.binding
 end

```

…equally nasty in implementation.

Ah well, clearly then I wasn’t missing something… there is no  
standard method.

Thanks,

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
New Zealand

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 29, 2008, 1:23pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/9 "2008-02-29T13:23:13Z")

</div>

Not that it is a lot nicer, but just another solution… and a little  
bit on side of what you wanted, but:

> > a = lambda{|c| “bra#{c}ket”}  
> > a.call(" see ")  
> > =\> “bra see ket”

🙂

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 29, 2008, 3:08pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/10 "2008-02-29T15:08:38Z")

</div>

If you want it to look even nicer, write it like:

> > a = lambda{|c| “bra#{c}ket”}  
> > a[" see "]  
> > =\> “bra see ket”

-Rob

On Feb 29, 2008, at 7:21 AM, Guby wrote:

> > Thanks,
> > 
> > John C. Phone : (64)(3) 358 6639  
> > Tait Electronics F

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 29, 2008, 6:32pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/11 "2008-02-29T18:32:50Z")

</div>

Sweet, that looks better!  
Good to learn something new! 🙂

S

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [February 29, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/string-interpolation-method/130403/12 "2008-02-29T19:28:14Z")

</div>

On Feb 29, 9:07 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> If you want it to look even nicer, write it like:
> 
> > > a = lambda{|c| “bra#{c}ket”}  
> > > a[" see "]  
> > > =\> “bra see ket”

The only trouble here is that you get the binding of where you defined  
the lambda, rather than the one in which you ultimately evaluate it  
in.

With 1.9 I think we can use instance\_exec to handle that however –  
that being the case, the String::interpolate method I demoed above  
could be improved.

T.
