0

I don't understand why my If else statement doesn't work properly, if I the input 'y' then it evaluates the last part of the if else statement.

puts('give 1: ')
nr1= Integer(gets)
puts('give 2: ')
nr2= Integer(gets)

selection = gets.to_s

if (selection == "y".upcase)
  puts "Result is #{nr1+nr2}"
elsif (selection =="v".upcase)
  puts "Result is #{nr1-nr2}"
else (selection == "k".upcase)
  puts "Result is #{nr1*nr2}"
end
3
  • 1
    else (selection == "k".upcase) will be treated as plain else. Probably you want elsif in place. Commented Jul 17, 2017 at 20:24
  • I did try switching it but it does not have any effect, it does not print the result if i input 'y' ,'v' or 'k'. Commented Jul 17, 2017 at 20:27
  • You want if selection.chomp.downcase == "y" (or if selection.chomp.upcase == "Y"). gets.to_s is the same as gets. gets always returns a string that ends with a newline ("\n"). You need gets.chomp to remove that newline. Commented Jul 17, 2017 at 20:32

3 Answers 3

4

A good habit to get into is breaking this out into a proper case statement:

case (selection.upcase)
when 'Y'
  puts "Result is #{nr1+nr2}"
when 'V'
  puts "Result is #{nr1-nr2}"
when 'K'
  puts "Result is #{nr1*nr2}"
else
  puts "I don't know what you mean."
end

Now it's important to note that selection == "y".upcase compares selection to "Y", it doesn't upcase your selection. I've adjusted that here do to a case-insensitive match on selection by using that as the thing the case is operating on.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, my problem was that I was inputting 'y' and the if statement was waiting for uppercase 'Y' which is why it did not execute.
Yeah, you almost had it, but things were a bit jumbled up. Hope this helped clarify.
@CarySwoveland A look-up table mapping string to operation and using send would be a better solution here, absolutely.
Another option is to use a hash. h = { 'Y'=>:+, 'V'=>:-, 'K'=>:* }; selection = gets.chomp.upcase; puts "Result is #{nr1.send(h[selection], nr2)}" if h.key?(selection).
@CarySwoveland You should probably add that as an alternate answer just for educational purposes.
1

You're missing #chomp method. As \n is appended to the STDIN which you can remove using #chomp.

2.2.2 :032 > selection = gets
y
 => "y\n"
2.2.2 :033 > selection.chomp
 => "y"

You can update your code as follows:

selection = gets.to_s.chomp.upcase

if selection == 'Y'
  puts "Result is #{nr1+nr2}"
elsif selection == 'V'
  puts "Result is #{nr1-nr2}"
elsif selection == 'K'
  puts "Result is #{nr1*nr2}"
else
  puts 'Invalid input'
end

Although, ruby case will suit best here. Something like:

case selection.upcase
when 'Y'
  puts "Result is #{nr1 + nr2}"
when 'V'
  puts "Result is #{nr1 - nr2}"
when 'K'
  puts "Result is #{nr1 * nr2}"
else
  puts "Invalid input. Please provide a valid input [Y/V/K]"
end

3 Comments

Can "y".upcase be simplified? Is that really what you want? Any why to_s in gets.to_s.chomp?
@CarySwoveland Yes, there's no need for #to_s. Updated the answer.
I'm pretty sure if selection.upcase == "Y" is what is intended here.
0

One option is to use a hash.

h = { 'Y'=>:+, 'V'=>:-, 'K'=>:* }
selection = gets.chomp.upcase
puts "Result is #{nr1.public_send(h[selection], nr2)}" if h.key?(selection)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.