Class: Cell
- Inherits:
-
Object
- Object
- Cell
- Defined in:
- lib/ruby-life.rb
Instance Attribute Summary collapse
-
#alive ⇒ Object
(also: #alive?)
Returns the value of attribute alive.
-
#neighbors ⇒ Object
Returns the value of attribute neighbors.
Instance Method Summary collapse
-
#initialize(status = nil, seed_probability = 0.5) ⇒ Cell
constructor
A new instance of Cell.
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_sym ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(status = nil, seed_probability = 0.5) ⇒ Cell
Returns a new instance of Cell.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby-life.rb', line 9 def initialize(status = nil, seed_probability = 0.5) case status when 'alive' @alive = true when 'dead' @alive = false else @alive = seed_probability > rand end end |
Instance Attribute Details
#alive ⇒ Object Also known as: alive?
Returns the value of attribute alive.
6 7 8 |
# File 'lib/ruby-life.rb', line 6 def alive @alive end |
#neighbors ⇒ Object
Returns the value of attribute neighbors.
6 7 8 |
# File 'lib/ruby-life.rb', line 6 def neighbors @neighbors end |
Instance Method Details
#to_i ⇒ Object
28 29 30 |
# File 'lib/ruby-life.rb', line 28 def to_i alive? ? 1 : 0 end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/ruby-life.rb', line 32 def to_s alive? ? 'O' : '-' end |
#to_sym ⇒ Object
36 37 38 |
# File 'lib/ruby-life.rb', line 36 def to_sym alive? ? :alive : :dead end |
#update ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ruby-life.rb', line 20 def update if alive? self.alive = false if neighbors < 2 || neighbors > 3 else self.alive = true if neighbors == 3 end end |