Skip to content

Commit

Permalink
ruby/clock: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Oct 29, 2023
1 parent e1909fe commit d1690bf
Show file tree
Hide file tree
Showing 10 changed files with 695 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Exercism Workspace
| July | Jurassic | C (5/5), C++ (5/5), Fortran (0/5) |
| August | Apps | Dart (5/5), Java (0/5), Kotlin (0/5) |
| September | Slimline | Awk (5/5), Bash\* (5/5), jq (0/5), Perl (0/5) |
| October | Object-Oriented | Ruby (1/5), Java (0/5) |
| October | Object-Oriented | Ruby (2/5), Java (0/5) |
| November | Nibbly | |
| December | Diversions | |

Expand Down
1 change: 1 addition & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@
- [word-count](./word-count/README.md)
- [allergies](./allergies/README.md)
- [simple-cipher](./simple-cipher/README.md)
- [clock](./clock/README.md)
7 changes: 6 additions & 1 deletion ruby/clock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ Two clocks that represent the same time should be equal to each other.

### Based on

Pairing session with Erin Drummond - https://twitter.com/ebdrummond
Pairing session with Erin Drummond - https://twitter.com/ebdrummond

### My Solution

- [my solution](./clock.rb)
- [run-tests](./run-tests-ruby.txt)
42 changes: 36 additions & 6 deletions ruby/clock/clock.rb
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
Loading

0 comments on commit d1690bf

Please sign in to comment.