Hi everyone! I am new to Ruby so I need help. Much appreciated!
My problem is this code right here:
puts “Heeeyyyy! I see you baby, can a G get to know yah?”
reply = gets.chomp
if reply == “no”.to_s or “hell no”.to_s or “nah”.to_s
puts ‘Why do you have to be so jacked up, I just want to get to know
you, that’s all?’
elsif reply == “yes”&& “sure”&& “maybe”
puts ‘hmmm I see you recognize a real man when you see one.’
else
puts ‘Okay okay we got of on the wrong foot, I will be respectful…I am
going to try this again.’
end
My questions is, when having two multiple conditions do I use && or ||
or .to_s between the words? With my current method, what ever I type, I
get the response from the second puts.(“Jacked up”). I want to be able
to type any of the options and get the proper response. Anyone please
help me how to craft multiple conditionals. Thank you
Your problem is that this is always true:
if reply == “no”.to_s or “hell no”.to_s or “nah”.to_s
That is:
if reply == “no”.to_s # This is a true/false result
or “hell no”.to_s # This will always return true, because “hell no” is
true.
You’re missing the comparison there.
You wanted:
if reply == “no” or reply == “hell no” or reply == “nah”
The “to_s” is unnecessary, console input is already a string.
With your current code, “No” is treated differently from “no”. You could
try gets.chomp.downcase to make all input lowercase.
It’s the accepted practice in Ruby to use && || rather than “and” “or”.
In this particular instance I’d probably use a “case” statement with
Regular Expressions to try and extract the content.
if reply == “no” or reply == “hell no” or reply == “nah”
The “to_s” is unnecessary, console input is already a string.
more relevant here: string literals like “no”, “nah”, …
are already strings!
With your current code, “No” is treated differently from “no”. You could
try gets.chomp.downcase to make all input lowercase.
It’s the accepted practice in Ruby to use && || rather than “and” “or”.
“and”, “or” have their use cases (control flow), but indeed,
for boolean expressions you should definitely use &&, ||.
(It will save you debugging time.)