Where does the output string come from?


#1

print " write something\n"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub(/s/, "th")
else print "no S\n"
end

  1. Note that I did not use ! after gsub that is user_input is not supposed to change in-place, but still its working.Can anyone enlighten me about how gsub is returning the string/value?
  2. I did not use any puts or print for the modified string. So how does this code prints the modified string?

#2

It's not part of the output, that's just the last instruction evaluated by Ruby (if you have an s in the string, if you don't, you'll get nil instead because print "no S\n" will return nil and will be the last instruction evaluated).

So no, user_input wasn't modified.

To check that you can just add a puts user_input at the end of your program, you'll see by yourself.