# Turning off interpolation in a regex

**URL:** <https://www.ruby-forum.com/t/turning-off-interpolation-in-a-regex/56421>\
**Category:** Ruby\
**Created:** [March 31, 2006, 3:34am UTC](https://www.ruby-forum.com/t/turning-off-interpolation-in-a-regex/56421 "2006-03-31T03:34:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [March 31, 2006, 3:34am UTC](https://www.ruby-forum.com/t/turning-off-interpolation-in-a-regex/56421/1 "2006-03-31T03:34:32Z")

</div>

I need to be able to find a run of characters (like _count_) in a  
string and substitute in the value #{object.count} so that I’m left  
with a string that can itself be interpolated. The method for doing  
this that jumped out was to use the string’s gsub method like this:

```
original_string.gsub(/\*count\*/, /count of #{object\.count}/

```

(Old hands at Ruby are now chuckling, and rightly so) How do I go  
about getting the literal text in the substitution portion without  
getting an, “NameError: undefined local variable or method `object’  
for main:Object”?

Any assistance, pointers or suggestions appreciated.

–  
Jason P.  
removed\_email\_address@domain.invalid

“The computer allows you to make mistakes  
faster than any other invention, with the  
possible exception of handguns and tequila.”

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [March 31, 2006, 4:53am UTC](https://www.ruby-forum.com/t/turning-off-interpolation-in-a-regex/56421/2 "2006-03-31T04:53:45Z")

</div>

Jason P. wrote:

> I need to be able to find a run of characters (like _count_) in a string  
> and substitute in the value #{object.count} so that I’m left with a  
> string that can itself be interpolated. The method for doing this that  
> jumped out was to use the string’s gsub method like this:
> 
> ```
> original_string.gsub(/\*count\*/, /count of #{object\.count}/
> 
> ```
> 
> (Old hands at Ruby are now chuckling, and rightly so) How do I go about  
> getting the literal text in the substitution portion without getting an,  
> “NameError: undefined local variable or method `object’ for main:Object”?

s = original\_string.gsub(/\*count\*/, ‘count of #{object.count}’)

# set object somehow

eval “”#{s}""

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [March 31, 2006, 5:08am UTC](https://www.ruby-forum.com/t/turning-off-interpolation-in-a-regex/56421/3 "2006-03-31T05:08:54Z")

</div>

On Mar 30, 2006, at 8:53 PM, Joel VanderWerf wrote:

> s = original\_string.gsub(/\*count\*/, ‘count of #{object.count}’)
> 
> # set object somehow
> 
> eval “”#{s}""

Thank you, Joel.

–  
Jason P.  
removed\_email\_address@domain.invalid

“The computer allows you to make mistakes  
faster than any other invention, with the  
possible exception of handguns and tequila.”
