class Passcard::Grid
Public Class Methods
new(arr)
click to toggle source
# File lib/passcard/grid.rb, line 9 def initialize(arr) @arr = arr raise_if_invalid_array! end
Public Instance Methods
==(g2)
click to toggle source
# File lib/passcard/grid.rb, line 14 def ==(g2) self.to_a == g2.to_a && g2.is_a?(self.class) end
[](*args)
click to toggle source
# File lib/passcard/grid.rb, line 18 def [](*args) self.rows_at(*args) end
alphanumeric?()
click to toggle source
# File lib/passcard/grid.rb, line 67 def alphanumeric? to_str =~ /\A[a-z0-9]+\z/i end
at(rows: [], cols: [])
click to toggle source
# File lib/passcard/grid.rb, line 48 def at(rows: [], cols: []) return self if rows.empty? && cols.empty? return self.rows_at(*rows) if cols.empty? return self.cols_at(*cols) if rows.empty? self.rows_at(*rows).cols_at(*cols) end
col_size()
click to toggle source
# File lib/passcard/grid.rb, line 26 def col_size @arr.any? ? @arr[0].size : 0 end
cols_at(*cols)
click to toggle source
# File lib/passcard/grid.rb, line 43 def cols_at(*cols) cols = cols.map{|a| a.respond_to?(:to_a) ? a.to_a : a} self.class.new @arr.map{|a| cols.flatten.map{|c| a[c % a.length]}} end
has_symbols?()
click to toggle source
# File lib/passcard/grid.rb, line 71 def has_symbols? return false if empty? return false if alphanumeric? (to_str.chars - ('!'..'~').to_a).empty? end
inspect()
click to toggle source
# File lib/passcard/grid.rb, line 107 def inspect chars = to_str[0..36] + (length > 36 ? "...." : "") chars = "" if length == 0 "#{self.class}[rows=#{row_size},cols=#{col_size},length=#{length}]{\"#{chars}\"}" end
length()
click to toggle source
# File lib/passcard/grid.rb, line 34 def length row_size * col_size end
numeric?()
click to toggle source
# File lib/passcard/grid.rb, line 63 def numeric? to_str =~ /\A[0-9]+\z/ end
print(*args)
click to toggle source
# File lib/passcard/grid.rb, line 101 def print(*args) options = args.last.is_a?(Hash) ? args.pop : {} puts(col_size > 40 || options[:concise] ? to_s.gsub(" ", '') : to_s) return self end
raise_if_invalid_array!()
click to toggle source
# File lib/passcard/grid.rb, line 114 def raise_if_invalid_array! valid = @arr.is_a?(Array) valid &&= @arr.reject{|r| r.is_a?(Array)}.empty? valid &&= @arr.join.length == @arr.size * (@arr[0].size rescue 0) raise Passcard::Error, "A Grid requires 2D array" unless valid end
rotate(r: 0, c: 0)
click to toggle source
Returns new grid by rotating self so that the element at cnt in self is the first element of the new grid. If cnt is negative then it rotates in the opposite direction.
# File lib/passcard/grid.rb, line 81 def rotate(r: 0, c: 0) self.class.new @arr.rotate(r).map{|row| row.rotate(c)} end
row_size()
click to toggle source
# File lib/passcard/grid.rb, line 22 def row_size @arr.size end
rows_at(*rows)
click to toggle source
# File lib/passcard/grid.rb, line 38 def rows_at(*rows) rows = rows.map{|a| a.respond_to?(:to_a) ? a.to_a : a} self.class.new rows.flatten.map{|a| @arr[a % @arr.size]} end
size()
click to toggle source
# File lib/passcard/grid.rb, line 30 def size [row_size, col_size] end
slice(coord1, coord2)
click to toggle source
# File lib/passcard/grid.rb, line 55 def slice(coord1, coord2) self.rows_at(coord1[0]...coord2[0]).cols_at(coord1[1]...coord2[1]) end
to_a()
click to toggle source
# File lib/passcard/grid.rb, line 85 def to_a @arr end
to_s()
click to toggle source
# File lib/passcard/grid.rb, line 97 def to_s @arr.map{|x| x.join(" ")}.join("\n") end
to_str()
click to toggle source
# File lib/passcard/grid.rb, line 89 def to_str @arr.join end
to_vstr()
click to toggle source
# File lib/passcard/grid.rb, line 93 def to_vstr transpose.to_str end
transpose()
click to toggle source
# File lib/passcard/grid.rb, line 59 def transpose self.class.new @arr.transpose end