Class: WordsMatrix::Matrix
- Inherits:
-
Object
- Object
- WordsMatrix::Matrix
- Defined in:
- lib/words_matrix/matrix.rb
Instance Attribute Summary collapse
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Instance Method Summary collapse
-
#initialize(n, min_length) ⇒ Matrix
constructor
A new instance of Matrix.
- #to_s ⇒ Object
- #words_from(x, y) ⇒ Object
Constructor Details
#initialize(n, min_length) ⇒ Matrix
Returns a new instance of Matrix.
4 5 6 7 8 9 |
# File 'lib/words_matrix/matrix.rb', line 4 def initialize(n, min_length) @n = n @min_length = min_length @grid = generate_grid parse_to_tokens! end |
Instance Attribute Details
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
2 3 4 |
# File 'lib/words_matrix/matrix.rb', line 2 def grid @grid end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
2 3 4 |
# File 'lib/words_matrix/matrix.rb', line 2 def tokens @tokens end |
Instance Method Details
#to_s ⇒ Object
11 12 13 14 15 |
# File 'lib/words_matrix/matrix.rb', line 11 def to_s grid.map do |line| line.join(" ") end.join("\n") end |
#words_from(x, y) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/words_matrix/matrix.rb', line 17 def words_from(x, y) raise ArgumentError, "Incorrect range" unless (0..@n-1).include?(x) || (0..@n-1).include?(y) [ horizontal_line((y...@n).to_a, x), horizontal_line((0..y).to_a.reverse, x), vertical_line((x...@n).to_a, y), vertical_line((0..x).to_a.reverse, y), diagonal((x...@n).to_a, y), diagonal((0..x).to_a.reverse, y), diagonal((x...@n).to_a, y, -1), diagonal((0..x).to_a.reverse, y, -1) ].flatten.compact end |