Module: TwistyPuzzles::ReversibleApplyable

Included in:
Algorithm, CompiledAlgorithm, StickerCycle, StickerCycles
Defined in:
lib/twisty_puzzles/reversible_applyable.rb

Overview

Module that makes a class that has an ‘apply_to` and `reverse` be able to apply temporarily.

Instance Method Summary collapse

Instance Method Details

#apply_temporarily_to(puzzle_state) ⇒ Object

Applies the current algorithm/cycle/whatever to the given puzzle state and yields the modified version. The puzzle state will be the same as the original after this function returns. Whether the yielded puzzle state is actually the same as the passed one or a copy is an implementation detail.



19
20
21
22
23
24
25
26
27
28
# File 'lib/twisty_puzzles/reversible_applyable.rb', line 19

def apply_temporarily_to(puzzle_state)
  return yield(apply_to_dupped(puzzle_state)) if with_dup_is_faster?(puzzle_state)

  apply_to(puzzle_state)
  begin
    yield(puzzle_state)
  ensure
    inverse.apply_to(puzzle_state)
  end
end

#apply_to_dupped(puzzle_state) ⇒ Object



8
9
10
11
12
# File 'lib/twisty_puzzles/reversible_applyable.rb', line 8

def apply_to_dupped(puzzle_state)
  dupped = puzzle_state.dup
  apply_to(dupped)
  dupped
end