SHARE
TWEET

Untitled

a guest Jun 24th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # File: calculator.rb
  2. #
  3. class Calculator
  4.   def initialize(a, b)
  5.     @a = a
  6.     @b = b
  7.   end
  8.  
  9.   def add
  10.     @a + @b
  11.   end
  12.  
  13.   def subtract
  14.     @a - @b
  15.   end
  16.  
  17.   def divide
  18.     @a / @b
  19.   end
  20.  
  21.   def multiply
  22.     @a * @b
  23.   end
  24. end
  25.  
  26. print 'Give me the first number: '
  27. a = gets.to_f
  28. print 'Give me the second number: '
  29. b = gets.to_f
  30. print 'Let me know which mathematical operation to carry out (1 for add, 2 for subtract, 3 for divide, 4 for multiply): '
  31. operation = gets.to_i
  32.  
  33. case operation
  34.   when 1
  35.     operation = :add
  36.   when 2
  37.     operation = :subtract
  38.   when 3
  39.     operation = :divide
  40.   when 4
  41.     operation = :multiply
  42.   else
  43.     puts "ERROR: You didn't give the correct operation"
  44.     exit(1)
  45. end
  46.  
  47. calculator = Calculator.new(a, b)
  48. result = calculator.public_send(operation)
  49.  
  50. puts "Result is: #{result}"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top