# Trying to use gsub using variables

**URL:** <https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320>\
**Category:** ruby-talk\
**Created:** [27 September 2008 02:50 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320 "2008-09-27T02:50:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Sheahan](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@John\_Sheahan](https://rubytalk.org/u/John_Sheahan)\
**Post date:** [27 September 2008 02:50 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/1 "2008-09-27T02:50:21Z")

</div>

I need to be able to match and substitute text in a string and I'm  
trying to use gsub with no luck....probably because it's looking for an  
regular expression.

Does anyone know how I can achieve this same thing using another method  
perhaps?  
Here is my code that is not working:

x = "NEW\_WORD"  
acl.gsub!(/WORD\_TO\_MATCH\_AND\_REPLACE/, x )

thanks

jackster

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

---

<div class="post-metadata">

**Author:** ![\_RK\_Sentinel](https://avatars.discourse-cdn.com/v4/letter/_/45deac/32.png) [@\_RK\_Sentinel](https://rubytalk.org/u/_RK_Sentinel)\
**Post date:** [27 September 2008 03:18 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/2 "2008-09-27T03:18:32Z")

</div>

jackster the jackle wrote:

> Here is my code that is not working:
> 
> x = "NEW\_WORD"  
> acl.gsub!(/WORD\_TO\_MATCH\_AND\_REPLACE/, x )

the x is fine, your text inside the /../ would be wrong.

e.g.  
irb(main):025:0\> u="Oct"  
=\> "Oct"  
irb(main):026:0\> s  
=\> "abc def Sep 10 10:22 filename"  
irb(main):027:0\> s.gsub(/Sep/,u)  
=\> "abc def Oct 10 10:22 filename"

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [27 September 2008 09:29 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/3 "2008-09-27T09:29:34Z")

</div>

How do you know? OP has neither posted what's in "acl" nor what he intends to replace.

&nbsp;&nbsp;robert

> **···**
>
> On 27.09.2008 05:18, Nit Khair wrote:
> 
> > jackster the jackle wrote:
> > 
> > > Here is my code that is not working:
> > > 
> > > x = "NEW\_WORD"  
> > > acl.gsub!(/WORD\_TO\_MATCH\_AND\_REPLACE/, x )
> > 
> > the x is fine, your text inside the /../ would be wrong.

---

<div class="post-metadata">

**Author:** ![John\_Sheahan](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@John\_Sheahan](https://rubytalk.org/u/John_Sheahan)\
**Post date:** [27 September 2008 12:22 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/4 "2008-09-27T12:22:55Z")

</div>

thanks guys...I'm making progress but what I have been slowly leading up  
to is searching using a variable then replacing it with a variable.

acl = "now is the time"  
x = time  
y = TEST

acl.gsub(/x/,y)

How do I get gsub to use the value of x rather "x" itself in my match?

thanks

Jackster

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

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [27 September 2008 12:54 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/5 "2008-09-27T12:54:15Z")

</div>

jackster the jackle wrote:

> thanks guys...I'm making progress but what I have been slowly leading up to is searching using a variable then replacing it with a variable.
> 
> acl = "now is the time"  
> x = time  
> y = TEST
> 
> acl.gsub(/x/,y)
> 
> How do I get gsub to use the value of x rather "x" itself in my match?
> 
> thanks
> 
> Jackster

/#{x}/

> **···**
>
> --  
> RMagick: [http://rmagick.rubyforge.org/](http://rmagick.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![John\_Sheahan](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@John\_Sheahan](https://rubytalk.org/u/John_Sheahan)\
**Post date:** [28 September 2008 01:03 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/6 "2008-09-28T01:03:43Z")

</div>

thanks...I got that to work...now for my last question...I am trying to  
match on a hash value. I have a text file that gets opened with 4  
different types of statements in it...the values are contained in my  
hash called "h". The values in the hash are (IN,OUT,PP,AA). @acl\_list  
contains a bunch of firewall rules that either have the value IN,OUT,PP  
or AA in them. What I am trying to do is loop through @acl\_list,  
substitute the value of h with the key (which works great) but then, I  
need to put the number 1 in the first firewall rule that contains IN,  
then the number 2 in the second firewall rule that contains IN etc.  
When I'm finished with the "IN" rules, I want to do the same thing for  
"OUT","PP" and "AA".

What I have here works great but only for the "IN" rules:

k= 1  
@acl\_list.each do |acl|  
&nbsp;&nbsp;h.each {|key,value|  
&nbsp;&nbsp;acl.gsub!(/#{key}/,value)}  
&nbsp;&nbsp;if acl[/IN/]  
&nbsp;&nbsp;&nbsp;&nbsp;acl.gsub!(/^/,k.to\_s)  
&nbsp;&nbsp;&nbsp;&nbsp;k+=1  
&nbsp;&nbsp;&nbsp;&nbsp;puts acl  
&nbsp;&nbsp;end

What I really need is to match on the value of the hash, then add the  
numbers to the beginning of each line. I thought the following would  
work but it doen't (although #{value} does have the correct info in it).  
Can anyone help me figure out how I can get the loop to match on the  
value of my hash? I think if I can get this to work, everything will be  
the way I need it.

k= 1  
@acl\_list.each do |acl|  
&nbsp;&nbsp;h.each {|key,value|  
&nbsp;&nbsp;acl.gsub!(/#{key}/,value)}  
&nbsp;&nbsp;if acl[/#{value}/]  
&nbsp;&nbsp;&nbsp;&nbsp;acl.gsub!(/^/,k.to\_s)  
&nbsp;&nbsp;&nbsp;&nbsp;k+=1  
&nbsp;&nbsp;&nbsp;&nbsp;puts acl  
&nbsp;&nbsp;end

Thanks again!  
jackster

Here's an example of what is actually contained in @acl\_list:  
access-list IN permit tcp 172.2.1.0 255.255.0.0 172.1.1.0 255.255.255.0  
eq ftp  
access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.10 255.255.255.0 eq  
13782  
access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0  
eq ftp  
access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0  
eq ftp  
access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0  
eq ftp  
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.255.0 eq  
www  
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq  
ftp  
access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.100.0 255.255.0.0 eq  
ssh  
access-list AA permit tcp 172.2.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq  
www  
access-list AA permit tcp 172.22.0.0 255.255.0.0 172.21.100.0 255.0.0.0  
eq 8080

My end result is that I want these same rules to look like this:  
1 access-list IN permit tcp 172.2.1.0 255.255.0.0 172.1.1.0  
255.255.255.0 eq ftp  
2 access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.10 255.255.255.0  
eq 13782  
3 access-list IN permit tcp 10.1.1.0 255.255.0.0 172.1.1.0 255.255.255.0  
eq ftp  
1 access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0  
255.255.255.0 eq ftp  
2 access-list OUT permit tcp 10.1.1.0 255.255.0.0 172.1.1.0  
255.255.255.0 eq ftp  
1 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.255.0  
eq www  
2 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq  
ftp  
3 access-list PP permit tcp 172.1.0.0 255.0.0.0 172.1.100.0 255.255.0.0  
eq ssh  
1 access-list AA permit tcp 172.2.0.0 255.0.0.0 172.1.1.0 255.255.0.0 eq  
www  
2 access-list AA permit tcp 172.22.0.0 255.255.0.0 172.21.1.0 255.0.0.0  
eq www

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 September 2008 08:49 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/7 "2008-09-28T08:49:19Z")

</div>

It seems you are doing this too complicated and making your life harder than necessary. If I read you correctly this is what you want to do: you want to read a file where each line falls in one of four \*fixed\* classes (IN, OUT...). You want to number each class individually and print that out.

If you use a Hash you can do something like this (proper variable naming goes a long way in making this readable and understandable):

counters = Hash.new 0

ARGF.each do |line|  
&nbsp;&nbsp;&nbsp;line\_class = line[/\b(?:IN|OUT|PP|AA)\b/]  
&nbsp;&nbsp;&nbsp;raise "Unknown rule" unless line\_class  
&nbsp;&nbsp;&nbsp;print counters[line\_class] += 1, " ", line  
end

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 28.09.2008 03:03, jackster the jackle wrote:
> 
> > thanks...I got that to work...now for my last question...I am trying to match on a hash value. I have a text file that gets opened with 4 different types of statements in it...the values are contained in my hash called "h". The values in the hash are (IN,OUT,PP,AA). @acl\_list contains a bunch of firewall rules that either have the value IN,OUT,PP or AA in them. What I am trying to do is loop through @acl\_list, substitute the value of h with the key (which works great) but then, I need to put the number 1 in the first firewall rule that contains IN, then the number 2 in the second firewall rule that contains IN etc.  
> > When I'm finished with the "IN" rules, I want to do the same thing for "OUT","PP" and "AA".
> > 
> > What I have here works great but only for the "IN" rules:
> > 
> > k= 1  
> > @acl\_list.each do |acl|  
> > &nbsp;&nbsp;h.each {|key,value|  
> > &nbsp;&nbsp;acl.gsub!(/#{key}/,value)}  
> > &nbsp;&nbsp;if acl[/IN/]  
> > &nbsp;&nbsp;&nbsp;&nbsp;acl.gsub!(/^/,k.to\_s)  
> > &nbsp;&nbsp;&nbsp;&nbsp;k+=1  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts acl  
> > &nbsp;&nbsp;end
> > 
> > What I really need is to match on the value of the hash, then add the numbers to the beginning of each line. I thought the following would work but it doen't (although #{value} does have the correct info in it). Can anyone help me figure out how I can get the loop to match on the value of my hash? I think if I can get this to work, everything will be the way I need it.
> > 
> > k= 1  
> > @acl\_list.each do |acl|  
> > &nbsp;&nbsp;h.each {|key,value|  
> > &nbsp;&nbsp;acl.gsub!(/#{key}/,value)}  
> > &nbsp;&nbsp;if acl[/#{value}/]  
> > &nbsp;&nbsp;&nbsp;&nbsp;acl.gsub!(/^/,k.to\_s)  
> > &nbsp;&nbsp;&nbsp;&nbsp;k+=1  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts acl  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 September 2008 09:19 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/8 "2008-09-28T09:19:20Z")

</div>

PS: Forgot to mention that you may have to adjust the regexp matching to be more robust. In your case you may want to do

line\_class = line[/^access-list\s+(IN|OUT|PP|AA)\b/, 1]

Cheers

&nbsp;&nbsp;robert

> **···**
>
> On 28.09.2008 10:46, Robert Klemme wrote:
> 
> > If you use a Hash you can do something like this (proper variable naming goes a long way in making this readable and understandable):
> > 
> > counters = Hash.new 0
> > 
> > ARGF.each do |line|  
> > &nbsp;&nbsp;line\_class = line[/\b(?:IN|OUT|PP|AA)\b/]  
> > &nbsp;&nbsp;raise "Unknown rule" unless line\_class  
> > &nbsp;&nbsp;print counters[line\_class] += 1, " ", line  
> > end

---

<div class="post-metadata">

**Author:** ![John\_Sheahan](https://avatars.discourse-cdn.com/v4/letter/j/ad7895/32.png) [@John\_Sheahan](https://rubytalk.org/u/John_Sheahan)\
**Post date:** [28 September 2008 10:07 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/9 "2008-09-28T10:07:39Z")

</div>

Thanks alot Robert....I'm not familiar with ARGF so I'm having a bit of  
trouble getting the code you sent properly inserted into what I already  
have.

What does the ARGF do?

thanks again

jackster

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

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [29 September 2008 05:55 UTC](https://rubytalk.org/t/trying-to-use-gsub-using-variables/49320/10 "2008-09-29T05:55:28Z")

</div>

What did you find out so far?

robert

> **···**
>
> 2008/9/28 jackster the jackle \<contact@thirdorder.net\>:
> 
> > Thanks alot Robert....I'm not familiar with ARGF so I'm having a bit of  
> > trouble getting the code you sent properly inserted into what I already  
> > have.
> > 
> > What does the ARGF do?
> 
> --  
> remember.guy do |as, often| as.you\_can - without end
