Calling multiple methods on a variable

Viewed 220

So I have the following code:

print "input please: "
user_input = gets.chomp.downcase!

if user_input.include? "s"
  user_input.gsub!(/s/, "th")
else 
  puts "There is no S in your input"
end

which throws me an error regarding the include method

when I run this it works:

print "input please "
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
  user_input.gsub!(/s/, "th")
else 
  puts "There is no S in your input"
end

is it not possible to call multiple methods on a variable?

1 Answers

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.

Related