Var.chomp.downcase! vs. var.chomp then var.downcase!


Why does this work

print "Who do you love"
user_input = gets.chomp

But this doesn't:

print "Who do you love"
user_input = gets.chomp.downcase!

The error is "undefined method `include?' for nil:NilClass"
They are followed by:

if user_input.include? "s"
    print "it has an s"

Replace this line with your code.


For this one to work a letter in user_input must be capital or the first letter.


Why does a letter have to be upper case for user_input = gets.chomp.downcase! to work?


because the downcase! method cannot work on a string that's already entirely in lowercase :slight_smile:


So it's actually not working or it's just not doing anything because there's nothing to do?



Which is why it is returning an error if the whole string is already lowercase letters why is it there in the first place


Sorry, I don't think I fully understand.

Both of these cases below, you're using downcase! on a string where you're not guaranteed that it will have an uppercase letter. But how come the first works and the second doesn't?

Case 1:

print "Who do you love"
user_input = gets.chomp

Case 2:

print "Who do you love"
user_input = gets.chomp.downcase!


The first one worked just fine for me. So did the second, they both lower-cased the entire sentence.


String#downcase! returns nil since its purpose is not to have a return value.

> nil.include? "s"
NoMethodError: undefined method `include?' for nil:NilClass


Try both with no lower-cased letters in your string. Only the first one works and not the second.


I believe the issue is with the !, after I tested a few things in irb.
Logically, you are defining a variable, and it will make it downcase
without the ! and preventing the error message.


When chaining methods, as here ...

user_input = gets.chomp.downcase!

... always be aware of what each method in the chain can and does return in all possible cases. See what @ionatan wrote previously, as well as downcase! → str or nil.

What happens with the above line if the user enters a lowercase string?


I'm really sorry that I'm still not catching what everyone seems to get. I read @ionatan's reply and the Ruby documentation and understand that if no changes are made, downcase! will not return a string but nil.

user_input = gets.chomp

If a user enters a lowercase string here, could you explain why in this above example's downcase! is return nil but it isn't throwing an error? In my head, downcase! will still try to modify user_input which is a lowercase string and realize that there is nothing to modify, return nil, then error as the gets.chomp.downcase! does.


The user_input is already set - the downcase! returns nil and therefore
does not change user_input. User_input still == gets.chomp (whatever user


From the perspective of the Ruby interpreter, the nil result is not itself an error. However, if you try to call a string method, such as include?, from user_input when it has the value, nil, that will constitute an error.