Class: Conway::CellLocationLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/conway/cell_location_lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CellLocationLookup

Returns a new instance of CellLocationLookup.



3
4
5
6
# File 'lib/conway/cell_location_lookup.rb', line 3

def initialize(&block)
  @hash  ||= block_given? ? Hash.new {|h,p| yield p } : {}
  @point ||= Point.new(0,0)
end

Instance Method Details

#countObject



23
24
25
# File 'lib/conway/cell_location_lookup.rb', line 23

def count
  locations.count
end

#each(&block) ⇒ Object



31
32
33
# File 'lib/conway/cell_location_lookup.rb', line 31

def each(&block)
  locations.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/conway/cell_location_lookup.rb', line 27

def empty?
  !(count > 0)
end

#insert(cell_location) ⇒ Object



8
9
10
# File 'lib/conway/cell_location_lookup.rb', line 8

def insert(cell_location)
  @hash[cell_location.point] = cell_location
end

#locationsObject



19
20
21
# File 'lib/conway/cell_location_lookup.rb', line 19

def locations
  @hash.values
end

#retrieve(point_or_x, y = nil) ⇒ Object



12
13
14
15
16
17
# File 'lib/conway/cell_location_lookup.rb', line 12

def retrieve(point_or_x, y=nil)
  if y
    point_or_x = @point.update(point_or_x, y)
  end
  @hash[point_or_x]
end