# Problems with replacing text

**URL:** <https://www.ruby-forum.com/t/problems-with-replacing-text/83023>\
**Category:** Ruby\
**Created:** [January 23, 2007, 3:16am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023 "2007-01-23T03:16:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 3:16am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023/1 "2007-01-23T03:16:33Z")

</div>

I am creating a small chatbot program in Ruby, and I need it to be able  
to change the word “I” to “You” when it is reading the user input. this  
is the code I am using:

uinput[‘I’] = ‘you’

The problem is, whenever I enter text that doesn’t include the word “I”,  
an error is printed out and the program terminates. I am looking for a  
way to work around this, possibly a command that ignores the error, or  
an If-Then statement that checks to see if “I” exists before replacing  
it.

Thank you!

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 3:52am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023/2 "2007-01-23T03:52:40Z")

</div>

On Jan 22, 2007, at 7:16 PM, Sam C. wrote:

> an If-Then statement that checks to see if “I” exists before replacing  
> it.

Try using uinput.gsub!(/I’, ‘you’)

alternatively, you can wrap your command in do/rescue/end, but that’s  
usually more work than you really want to do.

Dan

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 3:57am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023/3 "2007-01-23T03:57:47Z")

</div>

Dan Uznanski wrote:

> On Jan 22, 2007, at 7:16 PM, Sam C. wrote:
> 
> > an If-Then statement that checks to see if “I” exists before replacing  
> > it.
> 
> Try using uinput.gsub!(/I’, ‘you’)
> 
> alternatively, you can wrap your command in do/rescue/end, but that’s  
> usually more work than you really want to do.
> 
> Dan

Thank you so much, I now have it working!

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 4:07am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023/4 "2007-01-23T04:07:41Z")

</div>

Sam C. wrote:

> it.  
> irb(main):005:0\> input = “I see. In fact, I understand.”  
> =\> “I see. In fact, I understand.”  
> irb(main):006:0\> input.gsub!( /\bI\b/, “you” )  
> =\> “you see. In fact, you understand.”

---

<div class="post-metadata">

**Author:** ![samckayak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/58f4c7/32.png) [@samckayak](https://www.ruby-forum.com/u/samckayak)\
**Post date:** [January 23, 2007, 4:09am UTC](https://www.ruby-forum.com/t/problems-with-replacing-text/83023/5 "2007-01-23T04:09:42Z")

</div>

— Sam C. wrote:

> I am creating a small chatbot program in Ruby, and I need it  
> to be able to change the word “I” to “You” when it is reading  
> the user input. this is the code I am using:
> 
> uinput[‘I’] = ‘you’

You want to perform a “substitution” on the text contained in the  
variable. You can call the ‘gsub!’ method of the string object to  
perform a substitution. Here is an example from an irb session:

irb(main):001:0\> x = ‘I love Perl’  
=\> “I love Perl”  
irb(main):002:0\> x.gsub!(‘Perl’, ‘Ruby’)  
=\> “I love Ruby”  
irb(main):003:0\> puts x  
I love Perl

The code only version is:

x = ‘I love Perl’  
x.gsub!(‘Perl’, ‘Ruby’)  
puts x

There are several other problems you may run into with this as  
well. For example, what are you going to do when the user enters  
a lowercase i? Or what if a word contains the letter i? I  
recommend using a regular expression.

Regular Expressions:  
[http://www.rubycentral.com/book/tut\_stdtypes.html#S4](http://www.rubycentral.com/book/tut_stdtypes.html#S4)

Pattern-Based Substitution  
[http://www.rubycentral.com/book/tut\_stdtypes.html#UK](http://www.rubycentral.com/book/tut_stdtypes.html#UK)

Here is another irb session:

irb(main):004:0\> x = ‘I will eat pizza for dinner!’  
=\> “I will eat pizza for dinner!”  
irb(main):005:0\> x.gsub!(/\bi\b/i, ‘You’)  
=\> “You will eat pizza for dinner!”  
irb(main):006:0\> puts x  
You will eat pizza for dinner!  
=\> nil

Again, the code only version:

x = ‘I will eat pizza for dinner!’  
x.gsub!(/\bi\b/i, ‘You’)  
puts x
