# Using a variable with gsub & sub

**URL:** <https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233>\
**Category:** ruby-talk\
**Created:** [15 November 2007 21:34 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233 "2007-11-15T21:34:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Vanderhaden](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Peter\_Vanderhaden](https://rubytalk.org/u/Peter_Vanderhaden)\
**Post date:** [15 November 2007 21:34 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/1 "2007-11-15T21:34:16Z")

</div>

In the following statement, I'm replacing , with ,NULL,.

$inrec = $inrec.sub(',\\',',NULL,')

What I want to do is something like this:

$inrec = $inrec.sub(',\\',',$variable,')

Unfortunately, I haven't been able to figure out the syntax for using a  
variable with sub & gsub. Can someone help me out?  
Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![yermej](https://avatars.discourse-cdn.com/v4/letter/y/b2d939/32.png) [@yermej](https://rubytalk.org/u/yermej)\
**Post date:** [15 November 2007 22:00 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/2 "2007-11-15T22:00:06Z")

</div>

$inrec.gsub! ',\\', ",#{$variable},"

Though I'm not sure why you have the \\ in there, unless it just  
didn't end up on the newsgroup correctly.

> **···**
>
> On Nov 15, 3:34 pm, Peter Vanderhaden \<bostonanti...@yahoo.com\> wrote:
> 
> > In the following statement, I'm replacing , with ,NULL,.
> > 
> > $inrec = $inrec.sub(',\\',',NULL,')
> > 
> > What I want to do is something like this:
> > 
> > $inrec = $inrec.sub(',\\',',$variable,')
> > 
> > Unfortunately, I haven't been able to figure out the syntax for using a  
> > variable with sub & gsub. Can someone help me out?

---

<div class="post-metadata">

**Author:** ![Benjamin\_A\_Lee](https://avatars.discourse-cdn.com/v4/letter/b/4bbf92/32.png) [@Benjamin\_A\_Lee](https://rubytalk.org/u/Benjamin_A_Lee)\
**Post date:** [15 November 2007 22:16 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/3 "2007-11-15T22:16:33Z")

</div>

The replacement is simply a string, so you could do:

&nbsp;&nbsp;$inrec = $inrec.sub(',\\',','+$variable+',')

or

&nbsp;&nbsp;$inrec = $inrec.sub(',\\',',#{$variable},')

> **···**
>
> On Fri, Nov 16, 2007 at 06:34:16AM +0900, Peter Vanderhaden wrote:
> 
> > In the following statement, I'm replacing , with ,NULL,.
> > 
> > $inrec = $inrec.sub(',\\',',NULL,')
> > 
> > What I want to do is something like this:
> > 
> > $inrec = $inrec.sub(',\\',',$variable,')
> > 
> > Unfortunately, I haven't been able to figure out the syntax for using a  
> > variable with sub & gsub. Can someone help me out?  
> > Thanks.
> 
> --  
> Benjamin A'Lee :: benjamin.alee@subvert.org.uk  
> Subvert Technologies :: [http://subvert.org.uk/](http://subvert.org.uk/)

---

<div class="post-metadata">

**Author:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [16 November 2007 01:15 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/4 "2007-11-16T01:15:14Z")

</div>

Try this.

",#{var},"

Harry

> **···**
>
> On Nov 16, 2007 6:34 AM, Peter Vanderhaden \<bostonantifan@yahoo.com\> wrote:
> 
> > What I want to do is something like this:
> > 
> > $inrec = $inrec.sub(',\\',',$variable,')
> > 
> > Unfortunately, I haven't been able to figure out the syntax for using a  
> > variable with sub & gsub. Can someone help me out?
> 
> --  
> A Look into Japanese Ruby List in English
> 
> > **[Kakueki.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=kakueki.com)**
> >
> > Start using this domain right away. Straightforward domain shopping experience. Quick access to your domain.

---

<div class="post-metadata">

**Author:** ![Peter\_Vanderhaden](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@Peter\_Vanderhaden](https://rubytalk.org/u/Peter_Vanderhaden)\
**Post date:** [16 November 2007 01:24 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/5 "2007-11-16T01:24:17Z")

</div>

yermej,  
Your solution was exactly what I needed, thanks. As for the \\, that is  
part of the first value that is being replaced. The first backslash is  
an escape character for the second backshash.  
Thanks again,  
PETERV

yermej wrote:

> **···**
>
> > On Nov 15, 3:34 pm, Peter Vanderhaden \<bostonanti...@yahoo.com\> wrote:
> > 
> > > In the following statement, I'm replacing , with ,NULL,.
> > > 
> > > $inrec = $inrec.sub(',\\',',NULL,')
> > > 
> > > What I want to do is something like this:
> > > 
> > > $inrec = $inrec.sub(',\\',',$variable,')
> > > 
> > > Unfortunately, I haven't been able to figure out the syntax for using a  
> > > variable with sub & gsub. Can someone help me out?
> > 
> > $inrec.gsub! ',\\', ",#{$variable},"
> > 
> > Though I'm not sure why you have the \\ in there, unless it just  
> > didn't end up on the newsgroup correctly.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [16 November 2007 02:14 UTC](https://rubytalk.org/t/using-a-variable-with-gsub-sub/42233/6 "2007-11-16T02:14:00Z")

</div>

Peter Vanderhaden wrote:

> yermej,  
> Your solution was exactly what I needed, thanks. As for the \\, that is part of the first value that is being replaced. The first backslash is an escape character for the second backshash.  
> Thanks again,  
> PETERV
> 
> yermej wrote:  
> &nbsp;&nbsp;
> 
> > > In the following statement, I'm replacing , with ,NULL,.
> > > 
> > > $inrec = $inrec.sub(',\\',',NULL,')
> > > 
> > > What I want to do is something like this:
> > > 
> > > $inrec = $inrec.sub(',\\',',$variable,')
> > > 
> > > Unfortunately, I haven't been able to figure out the syntax for using a  
> > > variable with sub & gsub. Can someone help me out?  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> > 
> > $inrec.gsub! ',\\', ",#{$variable},"
> > 
> > Though I'm not sure why you have the \\ in there, unless it just  
> > didn't end up on the newsgroup correctly.  
> > &nbsp;&nbsp;&nbsp;&nbsp;

If you really want to use global variables ($var) then you can also do

$inrec.gsub!(',\\', ",#$variable,")

> **···**
>
> > > On Nov 15, 3:34 pm, Peter Vanderhaden \<bostonanti...@yahoo.com\> wrote:
