2DArray 0.1.0

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 +39 -0
  2. metadata +57 -0
@@ -0,0 +1,39 @@
1
+ class Array2D
2
+ def initialize(rows, cols, default=nil)
3
+ @stuff = Array.new(rows, []) {Array.new(cols, nil)}
4
+ end
5
+
6
+ # Redefined Object#method_missing as to forward the undefined
7
+ # method to +@stuff+, an array. You can even pass blocks, too,
8
+ # otherwise this would be pretty useless...
9
+ def method_missing(m, *args, &block)
10
+ if @stuff.respond_to?(m)
11
+ @stuff.flatten(1).send(m, *args) {|*b_args| block.call(*b_args)}
12
+ else
13
+ super(m, *args)
14
+ end
15
+ end
16
+
17
+ # Set the object at +x+, +y+ to +new+.
18
+ def []=(x, y, new)
19
+ # Initialize the array for +x+ if it hasn't already been.
20
+ if @stuff[x] == nil
21
+ @stuff[x] = []
22
+ end
23
+ @stuff[x][y] = new
24
+ end
25
+
26
+ # Returns the object at +x+, +y+.
27
+ def [](x, y)
28
+ # Return +nil+ if there is nothing at the given position.
29
+ if @stuff[x] == nil
30
+ return nil
31
+ end
32
+ @stuff[x][y]
33
+ end
34
+
35
+ # Returns if +thing+ is an element.
36
+ def include?(thing)
37
+ @stuff.flatten(1).include?(thing)
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: 2DArray
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - J. Wostenberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-12-03 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Adds 2-dimensional arrays to Ruby. Created for use in Rubydraw (https://rubygems.org/gems/rubydraw).
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/2DArray.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.6.2
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: 2-dimensional arrays in Ruby.
56
+ test_files: []
57
+