Using more methods on variable


#1

Hello, pretty new to Ruby. I noticed I can use more methods on a variable (e.g. variable.chomp.upcase!)

Although I got a problem in this exercise if branch part 1

This doesn’t work:

░print "Input your string: "
░user_input = gets.chomp.downcase!
░
░if user_input.include? "s"
░░ puts "Found that!"
░else
░ puts "Did not"
░end`

But this does:

░print "Input your string: "
░user_input = gets.chomp
░user_input.downcase!
░
░if user_input.include? "s"
░░ puts "Found that!"
░else
░░ puts "Did not"
░end

Why the first one throws nil?


#2

Using a bang! changes up the method to in situ and there is no return value, hence nil if nothing on the original object is altered.

.downcase!

will return nil when there are no upper case letters in the input. Either be sure to input at least one capital letter, or remove the bang.

.downcase

will return the original if no changes are made, or the altered value if changes are made.


#3

Got it, thank you sir.