Class: TwistyPuzzles::AbstractDirection

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/twisty_puzzles/abstract_direction.rb

Overview

Base class for directions.

Direct Known Subclasses

CubeDirection, SkewbDirection

Constant Summary collapse

POSSIBLE_DIRECTION_NAMES =
[[''], ['2', '2\''], ['\'', '3', '', '', '´', '`']].freeze
SIMPLE_DIRECTION_NAMES =
(['0'] + POSSIBLE_DIRECTION_NAMES.map(&:first)).freeze
POSSIBLE_SKEWB_DIRECTION_NAMES =
[['', '2\''], ['\'', '2']].freeze
SIMPLE_SKEWB_DIRECTION_NAMES =
(['0'] + POSSIBLE_SKEWB_DIRECTION_NAMES.map(&:first)).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ AbstractDirection

Returns a new instance of AbstractDirection.

Raises:

  • (TypeError)


12
13
14
15
16
17
18
19
# File 'lib/twisty_puzzles/abstract_direction.rb', line 12

def initialize(value)
  raise TypeError, "Direction value #{value} isn't an integer." unless value.is_a?(Integer)
  unless value >= 0 && value < self.class::NUM_DIRECTIONS
    raise ArgumentError, "Invalid direction value #{value}."
  end

  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



21
22
23
# File 'lib/twisty_puzzles/abstract_direction.rb', line 21

def value
  @value
end

Instance Method Details

#+(other) ⇒ Object



39
40
41
# File 'lib/twisty_puzzles/abstract_direction.rb', line 39

def +(other)
  self.class.new((@value + other.value) % self.class::NUM_DIRECTIONS)
end

#<=>(other) ⇒ Object



23
24
25
# File 'lib/twisty_puzzles/abstract_direction.rb', line 23

def <=>(other)
  @value <=> other.value
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


43
44
45
# File 'lib/twisty_puzzles/abstract_direction.rb', line 43

def eql?(other)
  self.class.equal?(other.class) && @value == other.value
end

#hashObject



49
50
51
# File 'lib/twisty_puzzles/abstract_direction.rb', line 49

def hash
  @value.hash
end

#inverseObject



35
36
37
# File 'lib/twisty_puzzles/abstract_direction.rb', line 35

def inverse
  self.class.new((self.class::NUM_DIRECTIONS - @value) % self.class::NUM_DIRECTIONS)
end

#non_zero?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/twisty_puzzles/abstract_direction.rb', line 31

def non_zero?
  @value.positive?
end

#zero?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/twisty_puzzles/abstract_direction.rb', line 27

def zero?
  @value.zero?
end