It is permissible (and recommended!) to chain methods in Ruby as you have done with gets.chomp.downcase!. However, using downcase! as opposed to the form downcase is causing an unexpected behavior in your code. According to the docs, the downcase! form
Downcases the contents of str, returning nil if no changes were made.
So if your input does not contain any upper case letters, downcase! returns nil and that causes an error down the line when you call .include? on it. Try it out with input like this string:
input please: No letter in here!
# prints
There is no S in your input
But the same called without the upper N errors:
input please: no letter in here!
Traceback (most recent call last):
test.rb:4:in `<main>': undefined method `include?' for nil:NilClass (NoMethodError)
If you supply input that does contain upper case characters, you'll get no error with your original code. The fix for this is to use the non-! form:
# Don't use the ! form of downcase
user_input = gets.chomp.downcase
Because the downcase! form is intended to modify a variable rather than modify a transient string such as the string returned by .chomp.
I suspect you intended to puts user_input there also, following your .gsub! call. Your string replacement s to th does work correctly if you add that in.
<main>': undefined methodinclude?' for nil:NilClass (NoMethodError)obj = gets.chomp.downcase.gsub!(/s/, "th"); puts 'There is no S in your input' if obj.nil?; obj. That's becausegsub!returnsnilif there is no match. (I'm not advocating this.)getscan already returnnil. (try hitting ctrl-d when prompted for input)