Class: Array2dSimple::Array2d
- Inherits:
-
Object
- Object
- Array2dSimple::Array2d
- Includes:
- Enumerable
- Defined in:
- lib/array_2d_simple/array_2d.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, value) ⇒ Object
- #each(&block) ⇒ Object
- #each_with_index ⇒ Object
-
#format2(value) ⇒ Object
Helper methods.
- #get_by_rc(r, c) ⇒ Object
- #get_by_xy(x, y) ⇒ Object
-
#initialize(params) ⇒ Array2d
constructor
A new instance of Array2d.
- #max ⇒ Object
- #min ⇒ Object
- #output ⇒ Object
- #project(&proc) ⇒ Object
- #set_by_rc(r, c, value) ⇒ Object
- #set_by_xy(x, y, value) ⇒ Object
- #set_each(&proc) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(params) ⇒ Array2d
Returns a new instance of Array2d.
8 9 10 11 12 13 14 15 16 |
# File 'lib/array_2d_simple/array_2d.rb', line 8 def initialize params raise 'width must be set' unless params[:width] raise 'height must be set' unless params[:height] params.each do |key,value| instance_variable_set("@#{key}",value) end @elements = Array.new(@height){ Array.new(@width) } end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
6 7 8 |
# File 'lib/array_2d_simple/array_2d.rb', line 6 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
6 7 8 |
# File 'lib/array_2d_simple/array_2d.rb', line 6 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
44 45 46 |
# File 'lib/array_2d_simple/array_2d.rb', line 44 def [] x,y get_by_xy(x,y) end |
#[]=(x, y, value) ⇒ Object
48 49 50 |
# File 'lib/array_2d_simple/array_2d.rb', line 48 def []= x,y, value set_by_xy(x,y,value) end |
#each(&block) ⇒ Object
98 99 100 |
# File 'lib/array_2d_simple/array_2d.rb', line 98 def each &block each_with_index &block end |
#each_with_index ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/array_2d_simple/array_2d.rb', line 90 def each_with_index (0..@width - 1).each do |y| (0..@height - 1).each do |x| yield(@elements[x][y],[x,y]) end end end |
#format2(value) ⇒ Object
Helper methods
103 104 105 |
# File 'lib/array_2d_simple/array_2d.rb', line 103 def format2 value "%0.2f" % value.round(2).to_s if value.finite? end |
#get_by_rc(r, c) ⇒ Object
22 23 24 25 |
# File 'lib/array_2d_simple/array_2d.rb', line 22 def get_by_rc r,c Printer.log 'Warning: #get_by_rc is depricated. Use get_by_xy. Note: arguments are reversed with get_by_xy' get_by_xy(c,r) end |
#get_by_xy(x, y) ⇒ Object
32 33 34 35 36 |
# File 'lib/array_2d_simple/array_2d.rb', line 32 def get_by_xy x,y raise 'x out of range' if x >= @width raise 'y out of range' if y >= @height @elements[y][x] end |
#max ⇒ Object
66 67 68 |
# File 'lib/array_2d_simple/array_2d.rb', line 66 def max @elements.dup.flatten.max end |
#min ⇒ Object
70 71 72 |
# File 'lib/array_2d_simple/array_2d.rb', line 70 def min @elements.dup.flatten.min end |
#output ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/array_2d_simple/array_2d.rb', line 52 def output size = format2(max.to_f).size output = '' (0..@height - 1).each do |r| (0..@width - 1).each do |c| output += "%#{ size + 4 }s" % format2(get_by_rc(r,c).to_f) if get_by_rc(r,c).respond_to? :to_f end output += "\n" end output end |
#project(&proc) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/array_2d_simple/array_2d.rb', line 74 def project &proc new_array = self.class.new( width: @width, height: @height ) new_array.set_each{ |x, y| proc.call( get_by_xy( y,x ))} new_array end |
#set_by_rc(r, c, value) ⇒ Object
27 28 29 30 |
# File 'lib/array_2d_simple/array_2d.rb', line 27 def set_by_rc r,c, value Printer.log 'Warning: #set_by_rc is depricated. Use set_by_xy. Note: arguments are reversed with set_by_xy' set_by_xy(c,r,value) end |
#set_by_xy(x, y, value) ⇒ Object
38 39 40 41 42 |
# File 'lib/array_2d_simple/array_2d.rb', line 38 def set_by_xy(x,y,value) raise 'x out of range' if x >= @width raise 'y out of range' if y >= @height @elements[y][x] = value end |
#set_each(&proc) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/array_2d_simple/array_2d.rb', line 82 def set_each &proc each_with_index do |v,i| x = i[0] y = i[1] @elements[x][y] = proc.call(x,y) end end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/array_2d_simple/array_2d.rb', line 18 def to_s "#{ self.class }: #{ @width }x#{ @height }" end |