Sha256: e4cbff9e82447806a6ed85bd10bea9781463cbee035f39981a085516d28924d6

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

class Array2D
  def initialize(rows, cols, default=nil)
    @stuff = Array.new(rows, []) {Array.new(cols, default)}
  end

  # Redefined Object#method_missing as to forward the undefined
  # method to +@stuff+, an array. You can even pass blocks, too,
  # otherwise this would be pretty useless...
  def method_missing(m, *args, &block)
    if @stuff.respond_to?(m)
      @stuff.flatten(1).send(m, *args) {|*b_args| block.call(*b_args)}
    else
      super(m, *args)
    end
  end

  # Set the object at +x+, +y+ to +new+.
  def []=(x, y, new)
    # Initialize the array for +x+ if it hasn't already been.
    if @stuff[x] == nil
      @stuff[x] = []
    end
    @stuff[x][y] = new
  end

  # Returns the object at +x+, +y+.
  def [](x, y)
    # Return +nil+ if there is nothing at the given position.
    if @stuff[x] == nil
      return nil
    end
    @stuff[x][y]
  end

  # Returns if +thing+ is an element.
  def include?(thing)
    @stuff.flatten(1).include?(thing)
  end

  def size
    @stuff.flatten(1).size
  end

  # Returns the number of rows.
  def width
    @stuff.size
  end

  # TODO: Implement a method to return the number of columns.
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
2DArray-0.1.5 lib/2DArray.rb