Stuck on If statement pt 1


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

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.>
https://www.codecademy.com/courses/learn-ruby/lessons/thith-meanth-war/exercises/setting-up-the-if-branch-part-

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

<do not remove the three backticks above>

#2

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


#3

Awesome that did it, thanks very much!

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


#4

If there are no capital letters it will return nil.
Ruby-doc#Downcase
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.

ie.

"foo".downcase => "foo"

but

"foo".downcase! => nil

#5

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,

"foo".downcase

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

#6

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