Daffy Duck, .include? not recognized?

print “Didn’t you want to tell me something?”
user_input = gets.chomp.downcase!
if user_input.include? “s”
print “Stop saying s”
print “Oh”

^^^ Here is my code.

Here is the output:
Didn’t you want to tell me something? yes
undefined method `include?’ for nil:NilClass

The lesson said that .include? should be a built in method but it says its not recognized. What gives?

Be sure that what you input has at least one uppercase letter else .downcase! will return nil.

1 Like

Wow thanks for the quick reply!

I thought ruby was supposed to be a flexible language! This seems very picky. What if I want it to be okay that some types in lowercase and use .downcase to ensure that it’s in lowercase? Am I ■■■■ out of luck?

In this case, if someone types in lowercase, it seems like it breaks the program? Sounds pretty unreasonable to me… :frowning:

I can’t give a good answer, just now, since like you, I need to read up more on this. There is a difference between downcase and downcase!, that much I know. Will dig up the docs on this topic and post a link, later.

1 Like

Thank you. You are super helpful! :grinning: Do you work for codecademy? I always wonder how people are so quick to be continuously helpful on these forums.

No, don’t work for CC. I’m one of about twenty volunteer moderators. There are dozens of knowledgeable members watching the forums.

1 Like

Class: String (Ruby 2.1.0)

The difference between downcase and downcase!

downcase makes a copy, then sets all ASCII letters to lowercase. We have to store the returned value in a new object. This is the one we should be using in the exercise since it doesn’t care if there are any changes or not.

downcase! changes the case in the object without making a copy. If no changes are made, it returns ``nil`.

1 Like

Your statement is incorrect.

a = “a”
puts a

I’m only paraphrasing what the documentation stated. “If no change, returns nil”. However, I do see that the above returns a. Perhaps you have some insight into this so we’re not flapping in the wind?

The example above is modifying a variable in place. The method downcase! returns no changes (nil).
“a”.downcase! will return nil
if a = “a”, a.downcase! remains “a” with no changes made to the string.

Another example:
my_string = “AbCdE”

This goes letter by letter to modify the contents (“a”, nil, “c”, nil, “e”) making my_string = “abcde”.

So the difference is using the downcase! method on a variable versus a string.

1 Like

Hello mr.mtf.
Currently i am learning ruby in codeacademy and i am in the thinth meneath war! Setting Up the ‘If’ Branch, Part 1.here is my code.
user_input = gets.chomp
if string_to_check.include? “s”
print “true”
print “false”
i built this program to check if there is any “s” in my program.but when i click save and submit, it says undefined local variable or method `string_to_check’ for #Context:0x13861d8

please check the problem in my program and please email me back.
thank you.

Try defining string_to_check as,

string_to_check = user_input.downcase

See if that works.

I just want to ask if you can suggest me any interpreter for ruby that i can download and use it for my daily revision in ruby, because I dont always want to get into codecademy and do my coding. It would also be great if you can provide a link to the website where you found the interpreter.

Ruby Docs: http://ruby-doc.org/

Ruby download: https://www.ruby-lang.org/en/downloads/

Unfortunately, I can’t give any advice beyond this. I had Ruby on my machine until it crashed and I had to get a new hard drive. Never put it back on since what little I do can be done here in the labs or in the course IDE.

A post was split to a new topic: Don’t know where is the problem