# How to do this

**URL:** <https://www.ruby-forum.com/t/how-to-do-this/196386>\
**Category:** Ruby\
**Created:** [October 12, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-do-this/196386 "2010-10-12T15:02:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [October 12, 2010, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-do-this/196386/1 "2010-10-12T15:02:57Z")

</div>

I don’t know how to explain so I will just code:

template = ‘some text #{variable}’ # single quotes for reason  
variable = ‘filled’  
res = some\_method(template)

Here, res should have value ‘some text filled’.

Is there an easy way doing this in ruby. Hard way is of course parsing  
and inserting text with my own method.

by  
TheR

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [October 12, 2010, 3:14pm UTC](https://www.ruby-forum.com/t/how-to-do-this/196386/2 "2010-10-12T15:14:28Z")

</div>

On Tue, Oct 12, 2010 at 8:02 AM, Damjan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> by  
> TheR
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Using ERB, here is a pretty straight forward cheatsheet/examples for  
you:  
[http://github.com/JoshCheek/JoshsRubyKickstart/blob/master/cheatsheets/erb-embedded\_ruby.rb](http://github.com/JoshCheek/JoshsRubyKickstart/blob/master/cheatsheets/erb-embedded_ruby.rb)

require ‘erb’

def some\_method(template)  
ERB.new( template , 0 , ‘\>’ ).result  
end

template = ‘some text \<%= variable %\>’  
variable = ‘filled’  
res = some\_method(template)

res # =\> “some text filled”

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [October 12, 2010, 3:15pm UTC](https://www.ruby-forum.com/t/how-to-do-this/196386/3 "2010-10-12T15:15:06Z")

</div>

On Tue, Oct 12, 2010 at 3:02 PM, Damjan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t know how to explain so I will just code:
> 
> template = ‘some text #{variable}’ # single quotes for reason  
> variable = ‘filled’  
> res = some\_method(template)
> 
> Here, res should have value ‘some text filled’.
> 
> Is there an easy way doing this in ruby. Hard way is of course parsing  
> and inserting text with my own method.

I would consider using double quotes an “easy way”. Either way, some  
piece of code needs to to the parsing. If you only ever want to fill  
in a single value you could as well use (s)printf.

Btw, parsing can be as easy as

def replace(template, values)  
template.gsub /(?\<!\)#{([^}]\*)}/ do  
values[$1]  
end  
end

template = ‘foo \#{bar}-#{bingo}-#{bongo}’  
x = replace template, “bar” =\> “1”, “bongo” =\> “2”  
p template, x

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [October 12, 2010, 3:27pm UTC](https://www.ruby-forum.com/t/how-to-do-this/196386/4 "2010-10-12T15:27:39Z")

</div>

On Tuesday 12 October 2010, Damjan R. wrote:

> |  
> |by  
> |TheR

You could do this:

#note the double quotes inside the single ones  
template = ‘“some text #{variable}”’  
variable = ‘filled’  
res = eval template

A better way could be to use ERB (included in the standard library):  
require ‘erb’  
template = ERB.new “some text \<%= variable %\>”  
variable = ‘filled’  
res = template.result

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![ther](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/977dab/32.png) [@ther](https://www.ruby-forum.com/u/ther)\
**Post date:** [October 13, 2010, 8:16am UTC](https://www.ruby-forum.com/t/how-to-do-this/196386/5 "2010-10-13T08:16:08Z")

</div>

Robert K. wrote in post #949495:

> Btw, parsing can be as easy as
> 
> def replace(template, values)  
> template.gsub /(?\<!\)#{([^}]\*)}/ do  
> values[$1]  
> end  
> end
> 
> template = ‘foo \#{bar}-#{bingo}-#{bongo}’  
> x = replace template, “bar” =\> “1”, “bongo” =\> “2”  
> p template, x

Robert thank you for this.

Ruby really is an amazing language. And after five years I still get  
puzzled how some things can be so simple.

by  
TheR
