Skip to content

Instantly share code, notes, and snippets.

@sebastianbenz
Last active August 29, 2015 13:57
Show Gist options
  • Save sebastianbenz/9632738 to your computer and use it in GitHub Desktop.
Save sebastianbenz/9632738 to your computer and use it in GitHub Desktop.
Programming without conditional statements.
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)
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))
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