Ruby "Thith Meanth War!" excersize "Setting Up the 'If' Branch, Part 2" error


#1

https://www.codecademy.com/courses/ruby-beginner-en-JdNDe/0/5
My code has this error

undefined method `include?' for nil:NilClass

print("input please:")
user_input=gets.chomp.downcase!
if user_input.include?("s")
    user_input.gsub!(/s/,"th")
end

#2

Delete the brackets :slight_smile:

from here

and here


#3

The parens are not an issue, and works fine. It's also how print works in Python 3, so an early start at practicing it will not hurt.


#4

When you press save & submit code, try to input the all letters CAPITAL


#5

Actually, as long as at least one letter is upper case the code will work. What will also work is removing the ! from capitalize! since it is being assigned, anyway.


#6

Shouldn't it be something else that has to do with the include?? 'Cause it's part of the error. I tried removing the parentheses, it didn't work.


#7

Try this code :

print "What is your name?"
user_input = gets.chomp.downcase!

if user_input.include? "s"
____userinput.gsub!(/s/,"th")
end

and when you press submit code button and write your name make the first letter Capital.


#8

@mohamadbahaa
I tried the code, and now it says this in the console:

What is your name? Eric
nil

And also this comes up:


But I did use .gsub!


#9

This seems to be the case. Unless I'm missing something downcase (and perhaps upcase) do not seem to be that useful if they will fail in the manner I described.

So, does this mean the downcase method (?) will fail if there is not at least one character to downcase?


#10

I had the same problem as printcoder_eric. However I took your advice and removed the ! from user_input = gets.chomp,downcase (not capitalize) and it has fixed the error. Thanks.


#11

My bad for being cryptic. Personally, I'd rather see someone work it through, as you have done, than just blurt out the actual solution.

Some testing shows what happens in each case (and I discovered that above I was wrong)...

str = "some string"
all = str
puts "> "+all
puts '====='
tolower = all.downcase!
puts tolower  # nil
puts '====='
tolower = str.downcase
puts tolower  # some string
puts '====='
ac = str.upcase
puts "> "+ac
puts '====='
allcaps = ac.upcase!
puts allcaps   # nil
puts '====='
allcaps = str.upcase
puts allcaps  # SOME STRING
puts '====='

cp = str.upcase
puts "> "+cp
puts '====='
caps = cp.capitalize!
puts caps   # Some string
puts '====='
caps = str.capitalize
puts caps   # Some string
puts '====='

cp = str.downcase
puts "> "+cp
puts '====='
caps = cp.capitalize!
puts caps   # Some string
puts '====='
caps = str.capitalize
puts caps   # Some string
puts '====='

Output

> some string
=====

=====
some string
=====
> SOME STRING
=====

=====
SOME STRING
=====
> SOME STRING
=====
Some string
=====
Some string
=====
> some string
=====
Some string
=====
Some string
=====
=> nil

The blank lines are nil.


#12

Now, I am having the same problem.


#13

This will not assign anything to user_input if there is not at least one capital letter in the user input. Suggest, remove the ! so it will work, regardless of inputs.


#14

Don't write ____ at the code and put space instead


#15

Remove the brackets after print and .include?.
Also try using the .downcase in a new line,so your code should look something like this:

print "What is your name?"
user_input = gets.chomp
user_input.downcase!

After submitting the code it doesn't matter if you submit in upper or lower case in the console.


#16

@darkwraith84
It worked!


#18