# How do I get a variable into a gsub?

**URL:** <https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069>\
**Category:** ruby-talk\
**Created:** [21 May 2003 02:35 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069 "2003-05-21T02:35:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Oshel](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@Dave\_Oshel](https://rubytalk.org/u/Dave_Oshel)\
**Post date:** [21 May 2003 02:35 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069/1 "2003-05-21T02:35:54Z")

</div>

Pardon the newbie question, but I can’t seem to find how to place the  
contents of a variable into the regexp field of a gsub.

That is:

A = “There is an N such that all such N should be in parentheses, so  
replace any N with (N).”

Ok, the specific case is trivial:

A.gsub( / N /, " (N) " )

But the general case is maddening. Obviously, I am missing something.

If I try:

B = “x” # the current thing I want to match and replace

Then:

A.gsub( Regexp.compile(B), " (#{B}) " ) ==\> error!

And so on…

How do I get a variable into that seemingly constant regular expression  
field?

TIA,

Dave

> **···**
>
> –  
> [“10110100101101000101000000100010100001100110111010010110001001100000010011000010011101000000010011110010110011100001011010100110001101101001000010010000001001101100011011110110110011100001011010100110001101100000001010110110100001101100011001110100110001101111011010110110010100001100001010100110001001101000011001001110000001000100101010000110000011101001011000100110110011100011010000000100100100101000001010010000000101100010111000101110000011100101110011110100111101000001011011110110101101101010011000001110100001101110011010100110011101001011011010000110110001100111010011000110111101101011011011110100001001101100011011110110110011100001011010100110001101101111010001010000010001001001101010100110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b\*”)

---

<div class="post-metadata">

**Author:** ![Wesley\_J\_Landaker](https://avatars.discourse-cdn.com/v4/letter/w/a698b9/32.png) [@Wesley\_J\_Landaker](https://rubytalk.org/u/Wesley_J_Landaker)\
**Post date:** [21 May 2003 02:40 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069/2 "2003-05-21T02:40:57Z")

</div>

Like this?

“one two three”.gsub(/(\w+)/,‘(\1)’) =\> “(one) (two) (three)”

> **···**
>
> On Tuesday 20 May 2003 8:35 pm, Dave Oshel wrote:
> 
> > Pardon the newbie question, but I can’t seem to find how to place the  
> > contents of a variable into the regexp field of a gsub.
> > 
> > That is:
> > 
> > A = “There is an N such that all such N should be in parentheses, so  
> > replace any N with (N).”
> 
> –  
> Wesley J. Landaker - [wjl@icecavern.net](mailto:wjl@icecavern.net)  
> OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [21 May 2003 08:36 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069/3 "2003-05-21T08:36:29Z")

</div>

What error? All the following worked under 1.7.3:

a.gsub(/#{b}/, “(#{b})”)  
a.gsub(Regexp.compile(b), “(#{b})”)  
a.gsub(Regexp.compile(b)) {|match| “(#{match})”}

martin

> **···**
>
> Dave Oshel [dcoshel@elide.spambaffler.mac.com](mailto:dcoshel@elide.spambaffler.mac.com) wrote:
> 
> > Pardon the newbie question, but I can’t seem to find how to place the  
> > contents of a variable into the regexp field of a gsub.
> > 
> > That is:
> > 
> > A = “There is an N such that all such N should be in parentheses, so  
> > replace any N with (N).”
> > 
> > Ok, the specific case is trivial:
> > 
> > A.gsub( / N /, " (N) " )
> > 
> > But the general case is maddening. Obviously, I am missing something.
> > 
> > If I try:
> > 
> > B = “x” # the current thing I want to match and replace
> > 
> > Then:
> > 
> > A.gsub( Regexp.compile(B), " (#{B}) " ) ==\> error!

---

<div class="post-metadata">

**Author:** ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)\
**Post date:** [21 May 2003 11:17 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069/4 "2003-05-21T11:17:10Z")

</div>

Hi –

> **···**
>
> On Wed, 21 May 2003, Dave Oshel wrote:
> 
> > B = “x” # the current thing I want to match and replace
> > 
> > Then:
> > 
> > A.gsub( Regexp.compile(B), " (#{B}) " ) ==\> error!
> > 
> > And so on…
> > 
> > How do I get a variable into that seemingly constant regular expression  
> > field?
> 
> The first step would be to use variables. Right now you’re using  
> constants 🙂
> 
> But either way, I can’t duplicate your error. Can you show some  
> runtime output?
> 
> David
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![Dave\_Oshel](https://avatars.discourse-cdn.com/v4/letter/d/b782af/32.png) [@Dave\_Oshel](https://rubytalk.org/u/Dave_Oshel)\
**Post date:** [21 May 2003 16:38 UTC](https://rubytalk.org/t/how-do-i-get-a-variable-into-a-gsub/6069/5 "2003-05-21T16:38:22Z")

</div>

In article  
[Pine.LNX.4.44.0305210714330.16607-100000@candle.superlink.net](mailto:Pine.LNX.4.44.0305210714330.16607-100000@candle.superlink.net),

> **···**
>
> [dblack@superlink.net](mailto:dblack@superlink.net) wrote:
> 
> > Hi –
> > 
> > On Wed, 21 May 2003, Dave Oshel wrote:
> > 
> > > B = “x” # the current thing I want to match and replace
> > > 
> > > Then:
> > > 
> > > A.gsub( Regexp.compile(B), " (#{B}) " ) ==\> error!
> > > 
> > > And so on…
> > > 
> > > How do I get a variable into that seemingly constant regular expression  
> > > field?
> > 
> > The first step would be to use variables. Right now you’re using  
> > constants 🙂
> > 
> > But either way, I can’t duplicate your error. Can you show some  
> > runtime output?
> > 
> > David
> 
> x.sub!(/#{var}/,“#{func(var)}”) works fine, I was boneheadedly  
> forgetting the !, then descending into baroque elaboration after  
> midnight.
> 
> Least surprise applies, but MY surprise is boundless sometimes 😉
> 
> –  
> [“10110100101101000101000000100010100001100110111010010110001001100000010011000010011101000000010011110010110011100001011010100110001101101001000010010000001001101100011011110110110011100001011010100110001101100000001010110110100001101100011001110100110001101111011010110110010100001100001010100110001001101000011001001110000001000100101010000110000011101001011000100110110011100011010000000100100100101000001010010000000101100010111000101110000011100101110011110100111101000001011011110110101101101010011000001110100001101110011010100110011101001011011010000110110001100111010011000110111101101011011011110100001001101100011011110110110011100001011010100110001101101111010001010000010001001001101010100110001011100000010010000010011101101111011000101110000101101010011001001110000001000100101010101110010001101001111000000100000100101000011011000110110101101010011001001110”].pack(“b\*”)
