Dan Uznanski
1/23/2007 2:41:00 AM
On Jan 22, 2007, at 7:16 PM, 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'
>
> 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