Class: ReversePolishCalculator::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/reverse-polish-calculator/input.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#valueObject (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

Returns:

  • (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

Returns:

  • (Boolean)


25
26
27
# File 'lib/reverse-polish-calculator/input.rb', line 25

def math_method?
  Math.respond_to?(value)
end

#operand?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/reverse-polish-calculator/input.rb', line 29

def operand?
  !!value[/\d/]
end

#to_fObject



33
34
35
# File 'lib/reverse-polish-calculator/input.rb', line 33

def to_f
  value.to_f
end