Problems with replacing text

S

Sam C.

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!
 
D

Dan Uznanski

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.

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
 
W

William James

Sam said:
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.

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."
 
S

Sam C.

Dan said:
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!
 
G

Glen Pfeiffer

--- Sam C. said:
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

Pattern-Based Substitution
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top