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


#1



Why does this work

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

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"
end


Replace this line with your code.


#2

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


#3

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


#4

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


#5

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


#6

Yes.

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


#7

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
user_input.downcase!

Case 2:

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

#8

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


#9

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

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

#10

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


#12

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.


#13

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?


#14

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
user_input.downcase!

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.


#15

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


#16

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.