# Delayed quote expansion

**URL:** <https://www.ruby-forum.com/t/delayed-quote-expansion/52708>\
**Category:** Ruby\
**Created:** [January 18, 2006, 7:06am UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708 "2006-01-18T07:06:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 7:06am UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/1 "2006-01-18T07:06:07Z")

</div>

I’m sure the answer to this will seem simple once I see it. Right now  
it eludes me.

How can one have a string object like this…

a = ‘#{some\_value}’

and have it get evaluated as a double quoted string (with the value for  
some\_value being automatically replaced) at a later time?

I would prefer that it is evaluated in context from which it is called.

I would like to do something like this…

def func(param)  
test = 42  
puts param.quote\_substitution  
end

func(‘my number is #{test}’) #=\> “my number is 42”

\_Kevin

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 8:14am UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/2 "2006-01-18T08:14:23Z")

</div>

On Jan 18, 2006, at 12:29 AM, Kevin O. wrote:

> func(‘my number is #{test}’) #=\> “my number is 42”  
> How about:

```
template = lambda { |x| "my number is: #{x}" }

# some time passes

template.call(42)	# "my number is: 42"

```

Gary W.

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 9:29am UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/3 "2006-01-18T09:29:39Z")

</div>

On 2006.01.18 14:29, Kevin O. wrote:

> I would prefer that it is evaluated in context from which it is called.
> 
> I would like to do something like this…
> 
> def func(param)  
> test = 42  
> puts param.quote\_substitution  
> end
> 
> func(‘my number is #{test}’) #=\> “my number is 42”

You can delay evaluation like this:

string = ‘"#{foo}"’

# …

value = eval string # Add a binding if you want

> \_Kevin

E

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/4 "2006-01-18T14:24:25Z")

</div>

Lots of good responses here, but the one that turns out to be the  
easiest is…

def func(param)  
some\_value = 42  
value = eval %Q{%Q{#{param}}}  
end

func(‘my number is #{some\_value}’) #=\> ‘my number is 42’

\_Kevin

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 11:48am UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/5 "2006-01-18T11:48:13Z")

</div>

On 1/18/06, Kevin O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
[snip]

> I would like to do something like this…
> 
> def func(param)  
> test = 42  
> puts param.quote\_substitution  
> end
> 
> func(‘my number is #{test}’) #=\> “my number is 42”

Try this:

class String  
def evaluate(\*args)  
eval inspect.gsub(/\#/, ‘#’), \*args  
end  
end

def func(str)  
x = 42  
str.evaluate(binding)  
end

puts func(‘the answer is #{x}’)

–

Paolo

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/6 "2006-01-18T15:28:04Z")

</div>

James G. wrote:

> What you really want here is ERB:

A good suggestion, but I would rather avoid unnecessary dependencies if  
there is an adequate pure ruby way to do it.

\_Kevin

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/7 "2006-01-18T15:19:47Z")

</div>

On Jan 18, 2006, at 6:48 AM, Kevin O. wrote:

> Lots of good responses here, but the one that turns out to be the  
> easiest is…
> 
> def func(param)  
> some\_value = 42  
> value = eval %Q{%Q{#{param}}}  
> end
> 
> func(‘my number is #{some\_value}’) #=\> ‘my number is 42’

What you really want here is ERB:

> > require “erb”  
> > =\> true
> 
> > def expand( template )  
> > some\_value = 42  
> > ERB.new(template).result(binding)  
> > end  
> > =\> nil
> 
> > expand “My number is \<%= some\_value %\>.”  
> > =\> “My number is 42.”

James Edward G. II

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/8 "2006-01-18T17:49:13Z")

</div>

Bob S. wrote:

> erb _is_ pure Ruby. It is part of the standard library, so every Ruby  
> installation has it.

So it is, my bad.

---

<div class="post-metadata">

**Author:** ![olbrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/5f9b8f/32.png) [@olbrich](https://www.ruby-forum.com/u/olbrich)\
**Post date:** [January 18, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/delayed-quote-expansion/52708/9 "2006-01-18T15:44:08Z")

</div>

Kevin O. wrote:

> James G. wrote:
> 
> > What you really want here is ERB:
> 
> A good suggestion, but I would rather avoid unnecessary dependencies if  
> there is an adequate pure ruby way to do it.

erb _is_ pure Ruby. It is part of the standard library, so every Ruby  
installation has it.
