Class: Subconv::Scc::Grid
- Inherits:
-
Array
- Object
- Array
- Subconv::Scc::Grid
- Defined in:
- lib/subconv/scc/reader.rb
Overview
Grid is just an array with some extra convenience functions and a default size
Instance Method Summary collapse
-
#clone ⇒ Object
Make a deep copy.
-
#empty? ⇒ Boolean
The grid is empty when there are no characters in it.
-
#initialize(*args) ⇒ Grid
constructor
A new instance of Grid.
-
#insert_text(row, column, text, style = CharacterStyle.default) ⇒ Object
Insert continuous text at a given position Returns self for chaining.
- #to_simple_text ⇒ Object
- #without_identical_characters(other_grid) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Grid
Returns a new instance of Grid.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/subconv/scc/reader.rb', line 18 def initialize(*args) if args.empty? super(GRID_ROWS) { Array.new(GRID_COLUMNS) } elsif args.size == 1 fail ArgumentError, 'Attempted to create grid from unsupported type' unless args[0].is_a?(Array) fail ArgumentError, 'Grid has illegal row count' if args[0].size != GRID_ROWS fail ArgumentError, 'Grid has illegal column count' if args[0].any? { |row| row.size != GRID_COLUMNS } super(args.first) else fail ArgumentError, 'Illegal number of parameters' end end |
Instance Method Details
#clone ⇒ Object
Make a deep copy
38 39 40 |
# File 'lib/subconv/scc/reader.rb', line 38 def clone Grid.new(map { |row| row.map(&:clone) }) end |
#empty? ⇒ Boolean
The grid is empty when there are no characters in it
33 34 35 |
# File 'lib/subconv/scc/reader.rb', line 33 def empty? flatten.compact.empty? end |
#insert_text(row, column, text, style = CharacterStyle.default) ⇒ Object
Insert continuous text at a given position Returns self for chaining
44 45 46 47 48 49 50 |
# File 'lib/subconv/scc/reader.rb', line 44 def insert_text(row, column, text, style = CharacterStyle.default) text.each_char do |char| self[row][column] = Character.new(char, style) column += 1 end self end |
#to_simple_text ⇒ Object
63 64 65 |
# File 'lib/subconv/scc/reader.rb', line 63 def to_simple_text map { |row| row.map { |char| char.nil? ? ' ' : char.character }.join }.join("\n") end |
#without_identical_characters(other_grid) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/subconv/scc/reader.rb', line 52 def without_identical_characters(other_grid) result = clone each_with_index do |row, row_i| row.each_with_index do |column, column_i| result[row_i][column_i] = nil if other_grid[row_i][column_i] == column end end result end |