Class: Array2D
Instance Attribute Summary collapse
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #==(o) ⇒ Object
- #[](x, y) ⇒ Object
- #[]=(x, y, value) ⇒ Object
- #column_size ⇒ Object
- #each(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
-
#initialize(rows, columns, value = nil) ⇒ Array2D
constructor
A new instance of Array2D.
- #row_size ⇒ Object
- #size ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(rows, columns, value = nil) ⇒ Array2D
Returns a new instance of Array2D.
5 6 7 |
# File 'lib/array_2d.rb', line 5 def initialize(rows, columns, value=nil) @state = Array.new(rows) { Array.new(columns) { value } } end |
Instance Attribute Details
#state ⇒ Object
Returns the value of attribute state.
3 4 5 |
# File 'lib/array_2d.rb', line 3 def state @state end |
Instance Method Details
#==(o) ⇒ Object
29 30 31 |
# File 'lib/array_2d.rb', line 29 def ==(o) o.class == self.class && o.state == state end |
#[](x, y) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/array_2d.rb', line 45 def [](x, y) case x when Integer case y when Integer @state[x][y] when Range if y.size <= column_size = Array.new(y.to_a.size) y.each {|yi| [yi - y.first] = @state[x][yi]} else raise IndexError, "Indices are out of range" end end when Range case y when Integer if x.size <= row_size = Array.new(x.to_a.size) x.each {|xi| [xi - x.first] = @state[xi][y]} else raise IndexError, "Indices are out of range" end when Range if x.size <= row_size && y.size <= column_size = Array2D.new(x.to_a.size, y.to_a.size) x.each do |xi| y.each do |yi| .state[xi - x.first][yi - y.first] = @state[xi][yi] end end else raise IndexError, "Indices are out of range" end end end end |
#[]=(x, y, value) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/array_2d.rb', line 86 def []=(x, y, value) case x when Integer case y when Integer @state[x][y] = value when Range if value.is_a?(Array) && y.size == value.size y.each {|yi| @state[x][yi] = value[yi - y.first]} elsif value.is_a?(Array) && y.size != value.size raise AssignmentError, "Value array is not the same size as subarray" else y.each {|yi| @state[x][yi] = value} end end when Range case y when Integer if value.is_a?(Array) && x.size == value.size x.each {|xi| @state[xi][y] = value[xi - x.first]} elsif value.is_a?(Array) && x.size != value.size raise AssignmentError, "Value array is not the same size as subarray" else x.each {|xi| @state[xi][y] = value} end when Range x.each do |xi| y.each do |yi| if value.is_a?(Array2D) && [x.size, y.size] == value.size @state[xi][yi] = value[xi - x.first, yi - y.first] elsif value.is_a?(Array2D) && [x.size, y.size] != value.size raise AssignmentError, "Value 2d array is not the same size as subarray" else @state[xi][yi] = value end end end end end end |
#column_size ⇒ Object
41 42 43 |
# File 'lib/array_2d.rb', line 41 def column_size @state[0].size end |
#each(&block) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/array_2d.rb', line 9 def each(&block) @state.each do |row| row.each do |e| yield e end end end |
#each_with_index(&block) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/array_2d.rb', line 17 def each_with_index(&block) @state.each_with_index do |row, row_index| row.each_with_index do |e, column_index| yield e, [row_index, column_index] end end end |
#row_size ⇒ Object
37 38 39 |
# File 'lib/array_2d.rb', line 37 def row_size @state.size end |
#size ⇒ Object
33 34 35 |
# File 'lib/array_2d.rb', line 33 def size [row_size, column_size] end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/array_2d.rb', line 25 def to_s @state.to_s end |