-
-
Save geofflane/2410000 to your computer and use it in GitHub Desktop.
Berlin Clock Kata
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
require 'time' | |
class BerlinClock | |
ON = '*' | |
OFF = '.' | |
def initialize(time) | |
@time = time | |
end | |
def counts | |
[ | |
@time.sec % 2, | |
@time.hour.divmod(5), | |
@time.min.divmod(5) | |
].flatten() | |
end | |
def format() | |
[ | |
make_line(counts[0], 1), | |
make_line(counts[1], 4), | |
make_line(counts[2], 4), | |
make_line(counts[3], 11), | |
make_line(counts[4], 4), | |
].join("\n") | |
end | |
def print() | |
puts format | |
end | |
private | |
def make_line(active, total) | |
ON * active + OFF * (total - active) | |
end | |
end | |
BerlinClock.new(Time.parse("2011-04-16 10:11:01 A.M.")).print() | |
puts | |
BerlinClock.new(Time.parse("2011-04-16 12:00:00")).print() | |
puts | |
BerlinClock.new(Time.parse("2011-04-16 23:59:59")).print() | |
puts | |
BerlinClock.new(Time.parse("2011-04-16 01:00:01")).print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment