2DArray 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/2DArray.rb +18 -2
  2. metadata +2 -2
@@ -1,6 +1,22 @@
1
1
  class Array2D
2
- def initialize(rows, cols, default=nil)
3
- @stuff = Array.new(rows, []) {Array.new(cols, default)}
2
+ def initialize(rows=0, cols=0, default=nil)
3
+ @stuff = Array.new(rows) {Array.new(cols, default)}
4
+ end
5
+
6
+ # Iterate through each element, while passing the object, its +x+ and +y+ position to the block.
7
+ def each
8
+ x = 0
9
+ y = 0
10
+ size.times do |s|
11
+ yield(self[x, y], x, y)
12
+ # Restart at +width - 1+ because the array starts at +0, 0+ not +1, 1+.
13
+ if x == width - 1
14
+ x = 0
15
+ y += 1
16
+ else
17
+ x += 1
18
+ end
19
+ end
4
20
  end
5
21
 
6
22
  # Redefined Object#method_missing as to forward the undefined
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: 2DArray
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.1.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - J. Wostenberg
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-12-03 00:00:00 -07:00
13
+ date: 2011-12-10 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies: []
16
16