class PictureFrame::Raster
Attributes
raster[R]
Public Class Methods
new(string_or_rows)
click to toggle source
# File lib/picture_frame/raster.rb, line 5 def initialize(string_or_rows) @raster = string_or_rows.is_a?(String) ? string_or_rows.split("\n") : string_or_rows trim_around! pad_short_rows! end
Public Instance Methods
==(other)
click to toggle source
# File lib/picture_frame/raster.rb, line 12 def ==(other) return unless other.is_a?(Raster) @raster == other.raster end
at(row, col)
click to toggle source
# File lib/picture_frame/raster.rb, line 37 def at(row, col) @raster[row][col] end
Also aliased as: []
bottom_right()
click to toggle source
# File lib/picture_frame/raster.rb, line 25 def bottom_right [dimensions[0] - 1, dimensions[1] - 1] end
dimensions()
click to toggle source
# File lib/picture_frame/raster.rb, line 17 def dimensions @dimensions ||= [@raster.size, (@raster.map { |r| r.length }.max || 0)] end
each(&block)
click to toggle source
# File lib/picture_frame/raster.rb, line 54 def each(&block) @raster.each(&block) end
each_with_index(&block)
click to toggle source
# File lib/picture_frame/raster.rb, line 58 def each_with_index(&block) @raster.each_with_index(&block) end
height()
click to toggle source
# File lib/picture_frame/raster.rb, line 29 def height dimensions[0] end
slice(top_left, bottom_right)
click to toggle source
# File lib/picture_frame/raster.rb, line 43 def slice(top_left, bottom_right) top_bottom = (top_left[0]..bottom_right[0]) left_right = (top_left[1]..bottom_right[1]) array = top_bottom.inject([]) do |rows, ri| rows << @raster[ri].slice(left_right) end self.class.new(array) end
to_s()
click to toggle source
# File lib/picture_frame/raster.rb, line 62 def to_s @raster.join("\n") end
top_left()
click to toggle source
# File lib/picture_frame/raster.rb, line 21 def top_left [0, 0] end
width()
click to toggle source
# File lib/picture_frame/raster.rb, line 33 def width dimensions[1] end
Private Instance Methods
pad_short_rows!()
click to toggle source
# File lib/picture_frame/raster.rb, line 78 def pad_short_rows! @raster.each do |r| r << ' ' * (width - r.length) end end
trim_around!()
click to toggle source
# File lib/picture_frame/raster.rb, line 72 def trim_around! # trim; extract and add side-trimming @raster.shift while @raster.first && @raster.first.empty? @raster.pop while @raster.last && @raster.last.empty? end