Class: Mathy::Operations::Operation
- Inherits:
-
Object
- Object
- Mathy::Operations::Operation
- Defined in:
- lib/mathy/operations/operation.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
- #calculate(operands) ⇒ Object
-
#initialize(difficulty, key, verification) ⇒ Operation
constructor
A new instance of Operation.
- #matches?(other_key) ⇒ Boolean
- #play_turn ⇒ Object
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
#key ⇒ Object (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
17 18 19 |
# File 'lib/mathy/operations/operation.rb', line 17 def matches?(other_key) key == other_key end |
#play_turn ⇒ Object
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 |