# String substitution

**URL:** <https://www.ruby-forum.com/t/string-substitution/103658>\
**Category:** Ruby\
**Created:** [May 8, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/string-substitution/103658 "2007-05-08T23:36:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![barrysfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@barrysfarm](https://www.ruby-forum.com/u/barrysfarm)\
**Post date:** [May 8, 2007, 11:36pm UTC](https://www.ruby-forum.com/t/string-substitution/103658/1 "2007-05-08T23:36:55Z")

</div>

Sorry if this question gets posted a lot. I’m unsure of the terminology,  
so my searches didn’t come up with anything.

I’ve got a block of html coming in from the database that looks like  
this:

#{@copy\_1}

#{@copy\_2}

#{@copy\_3}

I’d like to replace the variables with another string, which contains  
just text.

What’s the syntax for something like this? I got gsub! to do one  
variable, but it didn’t seem like it was the most efficient way to do  
it. Plus, I need to do multiple ones.

---

<div class="post-metadata">

**Author:** ![barrysfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@barrysfarm](https://www.ruby-forum.com/u/barrysfarm)\
**Post date:** [May 9, 2007, 12:01am UTC](https://www.ruby-forum.com/t/string-substitution/103658/2 "2007-05-09T00:01:09Z")

</div>

On May 8, 3:36 pm, Barry A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve got a block of html coming in from the database that looks like  
> this:
> 
> #{@copy\_1}

Does it have to look like that? Could it instead look like this?

 \<%=@copy\_1%\> 

If so, you want ERB. Roughly:

require ‘erb’  
template = #…stuff from the DB  
html = ERB.new( template ).result

---

<div class="post-metadata">

**Author:** ![barrysfarm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a698b9/32.png) [@barrysfarm](https://www.ruby-forum.com/u/barrysfarm)\
**Post date:** [May 9, 2007, 1:56am UTC](https://www.ruby-forum.com/t/string-substitution/103658/3 "2007-05-09T01:56:15Z")

</div>

On May 9, 2007, at 7:00 AM, Phrogz wrote:

> \<%=@copy\_1%\>
> 
> If so, you want ERB. Roughly:
> 
> require ‘erb’  
> template = #…stuff from the DB  
> html = ERB.new( template ).result

You might also consider moving the style attributes out of the div  
elements. Put them in one place. No use repeating the same thing  
again and again. Ideally even move it all to a style sheet outside of  
the html. More flexibility.
