Class: Mathy::Operations::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/mathy/operations/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(difficulty, key, verification) ⇒ Operation

Returns a new instance of Operation.


6
7
8
9
10
# File 'lib/mathy/operations/operation.rb', line 6

def initialize(difficulty, key, verification)
  @difficulty = difficulty
  @key = key
  @verifier = verification
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.


4
5
6
# File 'lib/mathy/operations/operation.rb', line 4

def key
  @key
end

Instance Method Details

#calculate(operands) ⇒ Object


21
22
23
# File 'lib/mathy/operations/operation.rb', line 21

def calculate(operands)
  operands.inject { |result, x| result.send(key.to_sym, x) }
end

#matches?(other_key) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
# File 'lib/mathy/operations/operation.rb', line 17

def matches?(other_key)
  key == other_key
end

#play_turnObject


12
13
14
15
# File 'lib/mathy/operations/operation.rb', line 12

def play_turn
  operands = @difficulty.next_operands
  @verifier.check_answer("#{operands.join(" #{key} ")} = ", calculate(operands))
end