Ruby - if else statement - ruby

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.

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

My case statement is not working, but the logic looks correct to me

This program prints the first statement, and exits after I enter a number e.g. "5", without printing anything else. From the logic I put in the case statement, I would expect it to output "You're not an adult :(" for 5. Other values lower than 120 do not work as expected either.
What is wrong?
print "Enter you age "
age = gets.chomp
if age.to_i<120
case age.to_i
when age.to_i<18
puts "You're not an adult :("
puts "Sorry"
when age.to_i>18
puts "You are now an adult!"
puts "phew"
end
end
Try this. Notice I've dropped the age.to_i from the case statement:
print "Enter you age "
age = gets.chomp
if age.to_i<120
case
when age.to_i<18
puts "You're not an adult :("
puts "Sorry"
when age.to_i>18
puts "You are now an adult!"
puts "phew"
end
end
EDIT
A little explanation is in order.
When you write this:
case foo
when "bar"
...
That essentially means:
if "bar" === foo
...
So your code was sort of like this:
if age.to_i<18 === age.to_i
...
which doesn't make a lot of sense. If you just write case with nothing after it, then it works more like a regular if statement. E.g.
case
when foo === "bar"
...
means roughly
if foo === "bar"
...
which is what you want. I hope that helps!
Here's a cleaned up version:
print "Enter you age "
age = gets.chomp.to_i
case age
when 0..18
puts "You're not an adult :("
puts "Sorry"
when 18..120
puts "You are now an adult!"
puts "phew"
else
puts "I think you're lying!"
end
Folding all of this into a single case statement and using ranges makes what's happening a lot more clear.

Ruby if statements 3 conditions

puts 'guess my favorite num'
x = gets.chomp
unless x.kind_of?(Fixnum)
puts "it's not a Numeric symbol"
if x=="2"
puts "Well done!"
if x!=2 || x.is_a?(Fixnum)
puts "Try more, dude"
end
end
end
Trying to learn ruby, but my code is not work :-( Need 3 DIFFERENT conditions for var. Where is a bug ?
Consider this:
#!/usr/bin/env ruby
puts "Guess my favorite num."
x = gets.chomp
begin
if Integer(x) == 2
puts "Well done!"
else
puts "Try more, dude."
end
rescue ArgumentError
puts "It's not an integer."
end
Semi-contrived example, but you're probably looking for elsif:
puts 'enter a favorite num'
x = gets.chomp.to_i
if x == 2
puts "you entered 2"
elsif x !=2
puts "you did not enter 2"
end
Also--as #Jan Dvorak points out--the gets method returns a string, which you would want to convert (to integer in this case).
Another solution would be to use a case statement:
print 'enter a favorite num'
x = gets.chomp.to_i
case x
when 2
puts "you entered 2"
else
puts "you did not enter 2"
end
You did probably mean something like that:
loop do
puts 'guess my favorite num'
x = gets.chomp
case x
when /\D/
puts "it's not a Numeric symbol"
when "2"
puts "Well done!"
break
else
puts "Try more, dude"
end
end

Ruby trouble with breaking loop

All right, I have the programming aptitude of a goldfish, so I could use some help. I have the following code (please excuse my terrible sense of humor):
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
while true
if answer != ('yes' || 'no')
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
elsif answer == ('yes' || 'no')
if answer == 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
break
if answer == 'no'
puts 'I guess you aren\'t cool enough.'
break
end
end
end
end
puts 'Well, smell ya later.'
It works mostly fine, but I have one problem: choosing the no option. I cannot figure out how to get that to work. It will loop fine, and choosing the yes option works.
Basically, my question is: how do I create a loop with two break options?
For something like this you should use a case/when which is a Ruby switch statement because having all of those if/end blocks will get confusing fast.
Also please read this guide: https://github.com/bbatsov/ruby-style-guide
It will teach you how to properly format your ruby code.
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
case answer
when 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
when 'no'
puts 'I guess you aren\'t cool enough.'
else
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
end
puts 'Well, smell ya later.'
And if you always want it to loop when they don't answer yes or no. You can do that by wrapping the code block in a loop as follows:
puts 'Do you have a middle name?'
answer=gets.chomp.downcase
loop do
case answer
when 'yes'
puts 'Cool. What is it?'
middlename=gets.chomp
puts middlename +'? That\'s dumb.'
break
when 'no'
puts 'I guess you aren\'t cool enough.'
break
else
puts 'Yes or no answers only, dumbass.'
puts 'So I\'ll ask again. Do you have a middle name?'
answer=gets.chomp.downcase
end
end
puts 'Well, smell ya later.'
See this answer: How to write a switch statement in Ruby
if answer != ('yes' || 'no') isn't doing what you think it is. Since 'yes' is non-nil, which is true, the logical OR is short-circuited and the value of the parenthetic expression is always 'yes'. Try if answer != 'yes' && answer != 'no' instead.

Variables in if/else statement won't work

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

Resources