Class: ReversePolishCalculator::Input
- Inherits:
-
Object
- Object
- ReversePolishCalculator::Input
- Defined in:
- lib/reverse-polish-calculator/input.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #exited? ⇒ Boolean
-
#initialize(value) ⇒ Input
constructor
A new instance of Input.
- #invoke(input_1, input_2 = nil) ⇒ Object
- #math_method? ⇒ Boolean
- #operand? ⇒ Boolean
- #to_f ⇒ Object
Constructor Details
#initialize(value) ⇒ Input
Returns a new instance of Input.
6 7 8 9 |
# File 'lib/reverse-polish-calculator/input.rb', line 6 def initialize(value) @value = value exit if exited? end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
4 5 6 |
# File 'lib/reverse-polish-calculator/input.rb', line 4 def value @value end |
Instance Method Details
#exited? ⇒ Boolean
11 12 13 |
# File 'lib/reverse-polish-calculator/input.rb', line 11 def exited? value.downcase == 'q' end |
#invoke(input_1, input_2 = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/reverse-polish-calculator/input.rb', line 15 def invoke(input_1, input_2 = nil) if math_method? Output.add "calculated: #{value}(#{input_1.to_f})" Math.send(value, input_1.to_f) else Output.add "calculated: #{input_1.to_f} #{value} #{input_2.to_f}" input_1.to_f.send(value, input_2.to_f) end end |
#math_method? ⇒ Boolean
25 26 27 |
# File 'lib/reverse-polish-calculator/input.rb', line 25 def math_method? Math.respond_to?(value) end |
#operand? ⇒ Boolean
29 30 31 |
# File 'lib/reverse-polish-calculator/input.rb', line 29 def operand? !!value[/\d/] end |
#to_f ⇒ Object
33 34 35 |
# File 'lib/reverse-polish-calculator/input.rb', line 33 def to_f value.to_f end |