Last active
August 29, 2015 13:57
-
-
Save sebastianbenz/9632738 to your computer and use it in GitHub Desktop.
Programming without conditional statements.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DeadCell | |
def to_s | |
' ' | |
end | |
end | |
class AliveCell | |
def to_s | |
'*' | |
end | |
end | |
def print_world(*cells) | |
cells.each do |cell| | |
print cell | |
end | |
end | |
print_world(AliveCell.new, DeadCell.new) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
serializers = { | |
:alive => 'x', | |
:dead => ' ' | |
} | |
class Cell | |
def initialize(state) | |
@state = state | |
end | |
def state? | |
@state | |
end | |
end | |
def print_world(serializers, *cells) | |
cells.each do |cell| | |
print serializers.fetch(cell.state) | |
end | |
end | |
print_world(serializers, Cell.new(:alive), Cell.new(:dead)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cell | |
def initialize(alive) | |
@alive = alive | |
end | |
def alive? | |
@alive | |
end | |
end | |
def print_world(*cells) | |
cells.each do |cell| | |
if cell.alive? | |
print 'x' | |
else | |
print ' ' | |
end | |
end | |
end | |
print_world(Cell.new(true), Cell.new(false)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment