-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_oop.rb
84 lines (73 loc) · 1.41 KB
/
calc_oop.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Calculator class
class Calculator
def initialize(number1, number2)
@num1 = number1
@num2 = number2
end
def sum
@num1 + @num2
end
def mult
@num1 * @num2
end
def divide
@num1 / @num2
end
def subtract
@num1 - @num2
end
def all_oper
arr = []
arr << sum
arr << subtract
arr << mult
arr << divide
arr
end
def obj_method
p methods - Calculator.methods
end
def self.method
p Calculator.methods - Class.methods
end
def list_oper
puts methods - self.class.methods
end
def check_oper(oper)
(methods - self.class.methods).include?(oper)
end
end
begin
puts 'Enter the first number'
num1 = gets.chomp
num1 = Integer(num1)
puts 'Enter the second number'
num2 = gets.chomp
num2 = Integer(num2)
rescue StandardError
puts 'Invalid input'
retry
end
obj = Calculator.new(num1, num2)
obj.list_oper
loop do
puts 'Enter the operation(Type all for all operations and stop to terminate)'
oper = gets.chomp
break if oper == 'stop'
# oper = gets.to_i
case oper
when '+'
puts "Addition: #{obj.sum}"
when '-'
puts "Subtraction: #{obj.subtract}"
when '*'
puts "Multiplication: #{obj.mult}"
when '/'
puts "Division: #{obj.divide}"
when 'all'
arr = obj.all_oper
puts "Addition: #{arr[0]},Subtraction: #{arr[1]},Multiplication: #{arr[2]},Division: #{arr[3]}"
else
puts 'Invalid input'
end
end