Variables in if/else statement won't work - ruby

I'm creating an interactive story, not a game. The options don't work in the if/else; here is the code.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if #{choice}==1
puts "you chose yes"
elsif #{choice}==2
puts "you chose no"
else
puts "Invalid Choice"
I tried leaving it intro, but that just calls the else statement, and with this setup, the tic tac toe guy in front of the brackets, it always calls the first option. please help.

if #{choice}==1 isn't how you test the value of variables. #{} is for string interpolation; you don't have to interpolate anything. Just use if choice == "1". Note that, because you're reading a string from the console, you need to compare against "1", not 1.
puts "Choose (1)yes or (2)no"
choice = gets.chomp
if choice == "1"
puts "you chose yes"
elsif choice == "2"
puts "you chose no"
else
puts "Invalid Choice"
end

Related

Enter with no input as a valid boolean

I'm writing some very simple code, asking for confirmation on a text input, and
what I want to do is that if the users simply presses "Enter", make it count as a "yes". For example:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || ''
other_method
else
method
end
end
But when I run it on Ruby, I get the "Warning, literal string in condition", and whatever you enter, calls the "other_method". The solution I found is the following:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y'
other_method
elsif correct == ''
other_method
else
method
end
end
But it's pretty annoying, I'd rather understand why the first one doesn't work, and how can I make it work using the | |
Thank you!
What the error is saying is that you are supplying a string (literal) inside of a conditional statement by itself. When you do if correct == "y" || "" you're actually telling it if correct == "y" OR "" and just supplying the string by itself is not a condition.
To fix this you'd simply supply the condition after the operator as well as before it. Ruby does not assume you want the same thing to happen after the ||.
Like this:
define method
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if correct == 'y' || correct == ''
other_method
else
method
end
end
Hope this helps. Happy coding
The solution here is to use Ruby's very versatile case statement to set up a number of "cases" you want to test:
puts "you entered '#{#text}', is it correct?"
case (gets.chomp)
when 'y', 'yes', ''
method_a
else
method_b
end
This can be extended to use regular expressions for even more versatility:
case (gets.chomp)
when /\A\s*y(?:es)?\s*\z/i
method_a
else
method_b
end
Where now anything like "y" or "yes" or "Yes " will work.
When you have bunch of if statements all testing the same variable, consider using a case statement to simplify your logic.
Here is another option using Regex (Docs):
puts "enter some text"
#text= gets.chomp
puts "you entered '#{#text}', is it correct?"
correct = gets.chomp
if /^y?$/ =~ correct # This will match 'y' and empty string both
other_method
else
method
end

Accept multiple input possibilities for a single conditional branch

I have this code:
choice = $stdin.gets.chomp
if choice == "yes"
puts "ok sure"
else
puts "try again"
end
I have more than one possibility of user input to put "ok sure". That is, if I input "ok", it should still say "ok sure".
Use case construction.
case choice
when "yes", "ok"
puts "ok sure"
else
puts "try again"
end
Easiest way would be to use Array#include? like so:
choice = $stdin.gets.chomp
if ["yes", "ok"].include? choice
puts "ok sure"
else
puts "try again"
end

Ruby - if else statement

I have been working on the configuration an else statement.
Here is the code:
puts "Do you like PC?"
case (gets.chomp)
when 'yes'
puts "I do too"
when 'no'
puts "Mac is better"
end
Now I am trying to configure it to (1) use else, and (2) to use elsif. How do I better write this to use "else", and how do I write it to use elsif. Here is what I wrote for the else version
puts "Do you like PC?"
case (gets.chomp)
if 'yes'
puts "I do too"
else
puts "Mac is better"
end
You should re-write as:
puts "Do you like PC?"
answer = gets.chomp
if answer == 'yes'
puts "I do too"
elsif answer == 'maybe'
puts "you're confused"
else
puts "Mac is better"
end
The keyword case is preserved for case statements.

Is there a a "back to the top" in Ruby? [duplicate]

This question already has answers here:
Is there goto statement in Ruby?
(4 answers)
Closed 8 years ago.
I'm kind of noob in ruby programming. My question is if there's something like anchors or goto in ruby, so I can create loops inside Ifs... Is there something like that?
Example:
anchorX
gets variable
if variable == "option one"
puts "you choose right"
else
puts "you choose wrong! DO IT AGAIN!"
go to anchorX
No, Ruby does not have goto. Try a loop instead:
loop {
input = gets.chomp
if input == "option one"
puts "you choose right"
break
else
puts "you choose wrong! DO IT AGAIN!"
end
}
Or, an alternative method (and arguably slightly more readable):
input = gets.chomp
until input == "option one"
puts "you choose wrong! DO IT AGAIN!"
input = gets.chomp
end
puts "you choose right"
Why would you want to use goto for something so trivial?
Use a while loop. The while syntax for ruby is:
while [condition] do
(put your code here)
end
For example:
gets variable
while variable != "option one" do
puts "you choose wrong!"
gets variable
puts "you choose right"
Besides loop statements, you can use retry statement to repeat entire begin/end block.
begin
input = gets.chomp
if input == "option one"
puts "you choose right"
else
puts "you choose wrong! DO IT AGAIN!"
raise
end
rescue
retry
end

Ending while loops in one method from another method in Ruby?

I'm working on a simple text-based dungeon game in Ruby, and I've run into a snag. Basically, I have one room with a locked door. The key is found in another room and I want the door to be unlocked once the key has been found.
Here's what I've got so far:
def chest_room
puts "You are in a small, round room."
puts "On the floor in front of you is a small wooden chest."
puts "It does not appear to be locked."
puts "What do you do?"
chest_open = false
while true
prompt; next_move = gets.chomp
if next_move == "open chest"
chest_open = true
puts "Inside the chest, you see a small, brass key."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "take key" and chest_open
key_present = true
puts "You take the key and slip it into a pocket."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "go back"
start()
else puts "I don't understand you."
puts "What do you do?"
prompt; next_move = gets.chomp
end
end
end
def start
puts "You find yourself in a dank room lit by torches."
puts "There are three doors leading out of here."
puts "What do you do?"
door_open = false
key_present = false
while true
prompt; next_move = gets.chomp
if next_move == "door 1"
chest_room()
elsif next_move == "door 2"
dais()
elsif next_move == "door 3" and not door_open
puts "This door is securely locked."
puts "You'll need to find some way of opening it before you can enter."
puts "What do you do?"
prompt; next_move = gets.chomp
elsif next_move == "door 3" and key_present
door_open = true
puts "The key you found fits easily into the lock."
puts "With a click, you unlock the door!"
orb_room()
else
puts "I don't understand you."
puts "What do you do?"
prompt; next_move = gets.chomp
end
end
end
Any input or advice? Essentially, I want to end the door_open = false loop once the key is found, but I can't figure out how to set door_open = true in the chest_room method then call it from the start method. Thanks!
What you really need is to back up and think out the design again. You have a Room, and it has a property "open" or "closed". You have a Key, and you need the key to change the state of that open/closed property.
Write out what you want to do, and think about how to model it as a Room and a Key, and you'll be on a better track.
(No, I don't want to give you the answer. Figuring it out is more important.)
You're having scope issues. make the door_open or key_present instance variables by putting an # in front of them. Then any method can access them. also case/when is cleaner than if elsif
while !#key_present # #key_present can be modified in another method
prompt; next_move = gets.chomp
case
when "door 1" == next_move then chest_room() # put constants first when comparing ==
when "door 2" == next_move then dais()
# more conditions
end
end

Resources