Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Calculator
- def initialize(*args)
- @num_ary = []; @oper_ary = []
- args.each do |arg|
- arg.is_a?(Numeric) ? @num_ary << arg : @oper_ary << arg
- end
- end
- def calculate
- result = nil
- @oper_ary.each do |oper|
- num1 = result.nil? ? @num_ary.shift : result
- num2 = @num_ary.shift
- result = num1.send(oper, num2)
- end
- result
- end
- end
- # Calculator.new(1, :+, 2, :*, 3, :/, 9, :-, 4).calculate
- # => -3
- # Calculator.new(3, :**, 2, :%, 4).calculate
- # => 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement