Daffy Duck, .include? not recognized?


#1

print "Didn't you want to tell me something?"
user_input = gets.chomp.downcase!
if user_input.include? "s"
print "Stop saying s"
else
print "Oh"
end

^^^ 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?


#2

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


#3

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:


#4

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.


#5

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.


#6

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


#7

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`.


#8

Your statement is incorrect.

a = "a"
a.downcase!
puts a


#9

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?


#10

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"
my_string.downcase!

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.


#11

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.
print"jJxbckjhbkc?"
user_input = gets.chomp
user_input.downcase
if string_to_check.include? "s"
print "true"
else
print "false"
end
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 #

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



#12

Try defining string_to_check as,

string_to_check = user_input.downcase

See if that works.


#14

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.


#15

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.


#16

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