Class: Sudoku::Coordinate
- Inherits:
-
Object
- Object
- Sudoku::Coordinate
- Includes:
- Comparable
- Defined in:
- lib/sudoku/model/coordinate.rb
Overview
Represents a cell coordinate in one dimension of the Sudoku grid.
Instance Attribute Summary collapse
-
#coordinate ⇒ Object
readonly
Returns the value of attribute coordinate.
Class Method Summary collapse
-
.valid?(coordinate) ⇒ Boolean
Will return true, if the given coordinate is between 0-8 interval, inclusive.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compares this cooridnate with the other one.
-
#coordinate_valid?(coordinate) ⇒ Boolean
Will check if given number can be used as the valid coordinate, which means, that it is a number in the interval 0-8 inclusive.
-
#eql?(other) ⇒ Boolean
Will check, if the given element represents the same one-dimensional coordiante.
-
#initialize(coordinate) ⇒ Coordinate
constructor
Will initialize this coordinate.
-
#to_i ⇒ Object
Will convert this coordinate to integer.
-
#to_int ⇒ Object
Will convert this coordinate to integer.
- #to_s ⇒ Object
Constructor Details
#initialize(coordinate) ⇒ Coordinate
Will initialize this coordinate.
-
coordinate
coordinate value, which must be in the 0-8 interval.
11 12 13 14 15 16 17 |
# File 'lib/sudoku/model/coordinate.rb', line 11 def initialize(coordinate) fail ArgumentError, "Coordinate value must be 0-8, value #{coordinate} given." unless coordinate_valid?(coordinate) @coordinate = coordinate.to_i end |
Instance Attribute Details
#coordinate ⇒ Object (readonly)
Returns the value of attribute coordinate.
7 8 9 |
# File 'lib/sudoku/model/coordinate.rb', line 7 def coordinate @coordinate end |
Class Method Details
.valid?(coordinate) ⇒ Boolean
Will return true, if the given coordinate is between 0-8 interval, inclusive.
30 31 32 33 |
# File 'lib/sudoku/model/coordinate.rb', line 30 def self.valid?(coordinate) return true if coordinate >= 0 && coordinate < 9 false end |
Instance Method Details
#<=>(other) ⇒ Object
Compares this cooridnate with the other one.
50 51 52 53 |
# File 'lib/sudoku/model/coordinate.rb', line 50 def <=>(other) return @coordinate <=> other.coordinate if other.respond_to?(:coordinate) @coordinate <=> other.to_i end |
#coordinate_valid?(coordinate) ⇒ Boolean
Will check if given number can be used as the valid coordinate, which means, that it is a number in the interval 0-8 inclusive. True is returned if the coordinate is valid, false otherwise.
-
coordinate
coordinate value to check
24 25 26 |
# File 'lib/sudoku/model/coordinate.rb', line 24 def coordinate_valid?(coordinate) Coordinate.valid?(coordinate) end |
#eql?(other) ⇒ Boolean
Will check, if the given element represents the same one-dimensional coordiante.
57 58 59 |
# File 'lib/sudoku/model/coordinate.rb', line 57 def eql?(other) @coordinate == other.to_i end |
#to_i ⇒ Object
Will convert this coordinate to integer.
40 41 42 |
# File 'lib/sudoku/model/coordinate.rb', line 40 def to_i @coordinate end |
#to_int ⇒ Object
Will convert this coordinate to integer.
45 46 47 |
# File 'lib/sudoku/model/coordinate.rb', line 45 def to_int to_i end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/sudoku/model/coordinate.rb', line 35 def to_s @coordinate.to_s end |