Returning the Final String—Er, "Thtring"


#1

I'm stuck with this, if anyone can help, please help me it's driving me crazy:

Here goes my code:


print "Say something: "
user_input= gets.chomp.downcase!
if user_input.include? "s"
user_input.gsub!(/s/,"th")
else
puts "#{user_input}"
end
my_string="muchachos"
puts "Adios, #{my_string}"

The console output is:
Say something: This
Adios, muchachos
nil

What i wanted to acomplish


Say something: This
thith
Adios, muchachos
nil

What is wrong with my code, im super confused???

#2

You'll need to puts the modified user_input.

Since there is an assignment being made, and to avoid nil returns, use,.downcase, not .downcase!.

Technically, you don't need an else. Just the if and its action. Then puts on the next line will print user_input whether changed or not.

if ...
    user_input.gsub(...)
puts "#(user_input}"

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.