Class: CellularMap::Map
- Inherits:
-
Object
- Object
- CellularMap::Map
- Includes:
- Enumerable
- Defined in:
- lib/cellular_map/map.rb
Overview
Map
Maps are limitless, only non-empty (nil content) cells are actually stored.
(see README for examples)
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Store of actually filled cells.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#[](x, y) ⇒ Object
Accessing a cell or a zone.
-
#[]=(x, y, content) ⇒ Object
Putting new content in a cell.
-
#each ⇒ Object
Iterating over each filled cell.
-
#empty! ⇒ Object
Empties the map filled cells.
-
#initialize ⇒ Map
constructor
:nodoc:.
-
#initialize_copy(other) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Map
:nodoc:
16 17 18 |
# File 'lib/cellular_map/map.rb', line 16 def initialize # :nodoc: @store = {} end |
Instance Attribute Details
#store ⇒ Object (readonly)
Store of actually filled cells.
14 15 16 |
# File 'lib/cellular_map/map.rb', line 14 def store @store end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
49 50 51 52 |
# File 'lib/cellular_map/map.rb', line 49 def ==(other) # :nodoc: @store.length == other.store.length && @store.collect { |k, v| other[*k].content == v }.uniq == [true] end |
#[](x, y) ⇒ Object
Accessing a cell or a zone.
21 22 23 24 25 26 27 |
# File 'lib/cellular_map/map.rb', line 21 def [](x, y) if x.respond_to?(:to_i) && y.respond_to?(:to_i) Cell.new(x, y, self) else Zone.new(x, y, self) end end |
#[]=(x, y, content) ⇒ Object
Putting new content in a cell.
30 31 32 33 34 35 36 |
# File 'lib/cellular_map/map.rb', line 30 def []=(x, y, content) if content.nil? @store.delete([x, y]) && nil else @store[[x, y]] = content end end |
#each ⇒ Object
Iterating over each filled cell.
39 40 41 42 |
# File 'lib/cellular_map/map.rb', line 39 def each # :yields: cell @store.keys.each { |k| yield Cell.new(*(k + [self])) } self end |
#empty! ⇒ Object
Empties the map filled cells.
45 46 47 |
# File 'lib/cellular_map/map.rb', line 45 def empty! @store = {} end |
#initialize_copy(other) ⇒ Object
:nodoc:
54 55 56 57 |
# File 'lib/cellular_map/map.rb', line 54 def initialize_copy(other) # :nodoc: @store = {} other.each { |c| self[c.x, c.y] = c.content } end |