# Simple gsub method

**URL:** <https://www.ruby-forum.com/t/simple-gsub-method/149979>\
**Category:** Ruby\
**Created:** [October 1, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979 "2008-10-01T22:43:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 1, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979/1 "2008-10-01T22:43:00Z")

</div>

I have simplified my method for searching test to the following:

def replacer(a, b)  
x = “now is the time for all good men to come to the aid of their  
country”  
x.gsub(/#{a}/, b)  
puts x  
end

puts "enter what to replace: "  
j = gets  
puts "enter replacer word: "  
h = gets

replacer(j, h)

Can someone help me determine why gsub can’t match my “a” value?

thanks

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 1, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979/2 "2008-10-01T22:48:00Z")

</div>

Alle Wednesday 01 October 2008, Jack S. ha scritto:

> j = gets  
> puts "enter replacer word: "  
> h = gets
> 
> replacer(j, h)
> 
> Can someone help me determine why gsub can’t match my “a” value?
> 
> thanks

Strings returned by gets end in a newline, which doesn’t exist in your  
string,  
so no match happens. To solve this, you can do

j = gets.chomp

and the same for h.

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 1, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979/3 "2008-10-01T22:52:36Z")

</div>

It does help Stefano as I had forgotton about \n so thanks for that but  
my script still does not match on my “a” value even with the chomp  
method added.

Stefano C. wrote:

> Alle Wednesday 01 October 2008, Jack S. ha scritto:
> 
> > j = gets  
> > puts "enter replacer word: "  
> > h = gets
> > 
> > replacer(j, h)
> > 
> > Can someone help me determine why gsub can’t match my “a” value?
> > 
> > thanks
> 
> Strings returned by gets end in a newline, which doesn’t exist in your  
> string,  
> so no match happens. To solve this, you can do
> 
> j = gets.chomp
> 
> and the same for h.
> 
> I hope this helps
> 
> Stefano

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 1, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979/4 "2008-10-01T22:58:01Z")

</div>

I take that back Stefano…you solved it for me!  
I forgot to add the x.gsub! …when I did, my script is working.

thanks a REAL lot for helping me with this!

j

Jack S. wrote:

> It does help Stefano as I had forgotton about \n so thanks for that but  
> my script still does not match on my “a” value even with the chomp  
> method added.
> 
> Stefano C. wrote:
> 
> > Alle Wednesday 01 October 2008, Jack S. ha scritto:
> > 
> > > j = gets  
> > > puts "enter replacer word: "  
> > > h = gets
> > > 
> > > replacer(j, h)
> > > 
> > > Can someone help me determine why gsub can’t match my “a” value?
> > > 
> > > thanks
> > 
> > Strings returned by gets end in a newline, which doesn’t exist in your  
> > string,  
> > so no match happens. To solve this, you can do
> > 
> > j = gets.chomp
> > 
> > and the same for h.
> > 
> > I hope this helps
> > 
> > Stefano

---

<div class="post-metadata">

**Author:** ![smithy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a4c791/32.png) [@smithy](https://www.ruby-forum.com/u/smithy)\
**Post date:** [October 1, 2008, 10:58pm UTC](https://www.ruby-forum.com/t/simple-gsub-method/149979/5 "2008-10-01T22:58:34Z")

</div>

Jack S. wrote:

> x.gsub(/#{a}/, b)

You don’t use the return value of gsub, so it is thrown away and this  
line  
basically does nothing. Calling gsub on x, does not change x (gsub!  
would).

HTH,  
Sebastian
