class Subconv::Scc::Grid

Grid is just an array with some extra convenience functions and a default size

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# 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

Public Instance Methods

clone() click to toggle source

Make a deep copy

# File lib/subconv/scc/reader.rb, line 38
def clone
  Grid.new(map { |row| row.map(&:clone) })
end
empty?() click to toggle source

The grid is empty when there are no characters in it

# File lib/subconv/scc/reader.rb, line 33
def empty?
  flatten.compact.empty?
end
insert_text(row, column, text, style = CharacterStyle.default) click to toggle source

Insert continuous text at a given position Returns self for chaining

# 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() click to toggle source
# 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) click to toggle source
# 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