1

I have the following code:

def say(msg)
  puts "=> #{msg}"
end

def do_math(num1, num2, operation)
  case operation
    when '+'
      num1.to_i + num2.to_i
    when '-'
      num1.to_i - num2.to_i
    when '*'
      num1.to_i * num2.to_i
    when '/'
      num1.to_f / num2.to_f
    end
end

say "Welcome to my calculator!"

run_calculator = 'yes'

while run_calculator == 'yes'
  say "What's the first number?"

  num1 = gets.chomp

  say "What's the second number?"

  num2 = gets.chomp

  say "What would you like to do?"

  say "Enter '+' for Addition, '-' for Subtraction, '*' for Multiplication, or '/' for Division"

  operation = gets.chomp

  if num2.to_f == 0 && operation == '/'
    say "You cannot devide by 0, please enter another value!"
    num2 = gets.chomp
  else
    result = do_math(num1, num2, operation)
  end

  say "#{num1} #{operation} #{num2} = #{result}"

  say "Would you like to do another calculation? Yes / No?"
  run_calculator = gets.chomp

  if run_calculator.downcase == 'no'
    say "Thanks for using my calculator!"
  elsif run_calculator.downcase == 'yes'
    run_calculator = 'yes'
  else
    until run_calculator.downcase == 'yes' || run_calculator.downcase == 'no'
      say "Please enter yes or no!"
      run_calculator = gets.chomp
    end
  end
end 

I need it to take the num1 and num2 variables that the user inputs and validate that they are numbers and return a message if they aren't.

I would like to use a Regex, but I don't know if I should create a method for this or just wrap it in a loop.

1
  • When supplying code, reduce it to the bare minimum needed to replicate the problem. In other words, something like num1 = gets.chomp would be sufficient, along with another line showing how you tried to test for a numeric. Commented Nov 10, 2014 at 21:08

2 Answers 2

4

The Integer method will raise an exception when the given string is not a valid number, whereas to_i will fail silently (which I think is not desired behavior):

begin
  num = Integer gets.chomp
rescue ArgumentError
  say "Invalid number!"
end

If you want a regex solution, this will also work (although I recommend the method above):

num = gets.chomp
unless num =~ /^\d+$/
  say "Invalid number!"
end
Sign up to request clarification or add additional context in comments.

Comments

0

You would often see each section written something like this:

ERR_MSG = "You must enter a non-negative integer"

def enter_non_negative_integer(instruction, error_msg)
  loop do
    puts instruction
    str = gets.strip
    return str.to_i if str =~ /^\d+$/
    puts error_msg
  end
end

x1 = enter_non_negative_integer("What's the first number?", ERR_MSG)
x2 = enter_non_negative_integer("What's the second number?", ERR_MSG)

Here's possible dialog:

What's the first number?
: cat
You must enter a non-negative integer
What's the first number?
: 4cat
You must enter a non-negative integer
What's the first number?
:             22
#=> 22
What's the second number?
: 51
#=> 51

x1 #=> 22
x2 #=> 51

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.