Class: Xudoku::Result
- Inherits:
-
Struct
- Object
- Struct
- Xudoku::Result
- Defined in:
- lib/xudoku.rb
Overview
Holds a matched puzzle and solution pair returned by the generator.
Instance Attribute Summary collapse
-
#puzzle ⇒ Board
The unsolved board with clues set.
-
#solution ⇒ Board
The fully solved board corresponding to the puzzle.
Instance Method Summary collapse
-
#to_json(*_args) ⇒ String
Serialises the result to a JSON object with
puzzleandsolutionkeys. -
#to_s ⇒ String
Returns a human-readable representation of the puzzle and solution.
Instance Attribute Details
#puzzle ⇒ Board
Returns the unsolved board with clues set.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/xudoku.rb', line 45 Result = Struct.new(:puzzle, :solution, keyword_init: true) do # Returns a human-readable representation of the puzzle and solution. # # @return [String] a labelled two-section string # # @example # puts result.to_s # # PUZZLE: # # ... board output ... # # SOLUTION: # # ... board output ... def to_s output = "PUZZLE:\n" output << puzzle.to_s output << "\n" output << "SOLUTION:\n" output << solution.to_s end # Serialises the result to a JSON object with +puzzle+ and +solution+ keys. # # The +*_args+ splat is accepted for compatibility with <tt>JSON.generate</tt> # and <tt>to_json</tt> conventions but is otherwise unused. # # @return [String] a JSON string of the form <tt>{"puzzle":...,"solution":...}</tt> def to_json(*_args) { puzzle: puzzle, solution: solution }.to_json end end |
#solution ⇒ Board
Returns the fully solved board corresponding to the puzzle.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/xudoku.rb', line 45 Result = Struct.new(:puzzle, :solution, keyword_init: true) do # Returns a human-readable representation of the puzzle and solution. # # @return [String] a labelled two-section string # # @example # puts result.to_s # # PUZZLE: # # ... board output ... # # SOLUTION: # # ... board output ... def to_s output = "PUZZLE:\n" output << puzzle.to_s output << "\n" output << "SOLUTION:\n" output << solution.to_s end # Serialises the result to a JSON object with +puzzle+ and +solution+ keys. # # The +*_args+ splat is accepted for compatibility with <tt>JSON.generate</tt> # and <tt>to_json</tt> conventions but is otherwise unused. # # @return [String] a JSON string of the form <tt>{"puzzle":...,"solution":...}</tt> def to_json(*_args) { puzzle: puzzle, solution: solution }.to_json end end |
Instance Method Details
#to_json(*_args) ⇒ String
Serialises the result to a JSON object with puzzle and solution keys.
The *_args splat is accepted for compatibility with JSON.generate and to_json conventions but is otherwise unused.
70 71 72 |
# File 'lib/xudoku.rb', line 70 def to_json(*_args) { puzzle: puzzle, solution: solution }.to_json end |
#to_s ⇒ String
Returns a human-readable representation of the puzzle and solution.
56 57 58 59 60 61 62 |
# File 'lib/xudoku.rb', line 56 def to_s output = "PUZZLE:\n" output << puzzle.to_s output << "\n" output << "SOLUTION:\n" output << solution.to_s end |