Stuck on If statement pt 1



I’m getting error “undefined method ‘include?’ for nil:NilClass” when creating first part of If statement. I have tried adding a print command to the line just above END but it still gives the same error.

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
undefined method `include?’ for nil:NilClass


print "Please enter your statement."
user_input = gets.chomp.downcase!

if user_input.include? "s"

<do not remove the three backticks above>


Your user_input has to contain at least one capital letter or downcase! will return nil.


Awesome that did it, thanks very much!

Just wondering, is that error triggering because downcase! is expecting at least one capital letter?


If there are no capital letters it will return nil.
When used with a bang(!) it will return nil if there are no changes. Using downcase without the bang will return a copy of the original string with or without any changes.


"foo".downcase => "foo"


"foo".downcase! => nil


That is the crucial bit of information needed to understand any Ruby bang (in-place) operation. They all have the same possible outcomes… The object was changed, or as unchanged, discarded, hence nil. There is nothing in the result.

Without the bang, the original is unchanged and only the copy is modified (or not). That is why we always need a variable to assign the result to.

Techinically nothing happens unless there is an assignment…

foo = "foo".downcase

The above is a statement, the following is an expression,


We can only see the value of an expression if we assign it to something.

6 + 7

Also an expression but we do not see what it amounts to without some form of assignment (or print statement)

print "foo".downcase
print 6 + 7


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.