-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
695 additions
and
95 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,37 @@ | ||
=begin | ||
Write your code for the 'Clock' exercise in this file. Make the tests in | ||
`clock_test.rb` pass. | ||
# frozen_string_literal: false | ||
|
||
To get started with TDD, see the `README.md` file in your | ||
`ruby/clock` directory. | ||
=end | ||
# https://exercism.org/tracks/ruby/exercises/clock | ||
# Clock exercise | ||
class Clock | ||
attr_reader :hour, :minute | ||
|
||
MINUTES_IN_HOUR = 60 | ||
HOURS_IN_DAY = 24 | ||
|
||
def initialize(hour: 0, minute: 0) | ||
@hour, @minute = normalize(hour, minute) | ||
end | ||
|
||
def normalize(hour, minute) | ||
total_minutes = hour * MINUTES_IN_HOUR + minute | ||
total_hours = total_minutes / MINUTES_IN_HOUR | ||
|
||
[total_hours.modulo(HOURS_IN_DAY), total_minutes.modulo(MINUTES_IN_HOUR)] | ||
end | ||
|
||
def to_s | ||
"#{@hour.to_s.rjust(2, '0')}:#{@minute.to_s.rjust(2, '0')}" | ||
end | ||
|
||
def +(other) | ||
Clock.new(hour: @hour + other.hour, minute: @minute + other.minute) | ||
end | ||
|
||
def -(other) | ||
Clock.new(hour: @hour - other.hour, minute: @minute - other.minute) | ||
end | ||
|
||
def ==(other) | ||
@hour == other.hour && @minute == other.minute | ||
end | ||
end |
Oops, something went wrong.