🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of remcopeereboom

remcopeereboom's solution

to Clock in the Ruby Track

Published at Jul 13 2018 · 11 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement a clock that handles times without dates.

You should be able to add and subtract minutes to it.

Two clocks that represent the same time should be equal to each other.


For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby clock_test.rb

To include color from the command line:

ruby -r minitest/pride clock_test.rb

Source

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

clock_test.rb

require 'minitest/autorun'
require_relative 'clock'

# Common test data version: 1.0.1 54c3b74
class ClockTest < Minitest::Test
  def test_on_the_hour
    # skip
    assert_equal "08:00", Clock.at(8, 0).to_s
  end

  def test_past_the_hour
    skip
    assert_equal "11:09", Clock.at(11, 9).to_s
  end

  def test_midnight_is_zero_hours
    skip
    assert_equal "00:00", Clock.at(24, 0).to_s
  end

  def test_hour_rolls_over
    skip
    assert_equal "01:00", Clock.at(25, 0).to_s
  end

  def test_hour_rolls_over_continuously
    skip
    assert_equal "04:00", Clock.at(100, 0).to_s
  end

  def test_sixty_minutes_is_next_hour
    skip
    assert_equal "02:00", Clock.at(1, 60).to_s
  end

  def test_minutes_roll_over
    skip
    assert_equal "02:40", Clock.at(0, 160).to_s
  end

  def test_minutes_roll_over_continuously
    skip
    assert_equal "04:43", Clock.at(0, 1723).to_s
  end

  def test_hour_and_minutes_roll_over
    skip
    assert_equal "03:40", Clock.at(25, 160).to_s
  end

  def test_hour_and_minutes_roll_over_continuously
    skip
    assert_equal "11:01", Clock.at(201, 3001).to_s
  end

  def test_hour_and_minutes_roll_over_to_exactly_midnight
    skip
    assert_equal "00:00", Clock.at(72, 8640).to_s
  end

  def test_negative_hour
    skip
    assert_equal "23:15", Clock.at(-1, 15).to_s
  end

  def test_negative_hour_rolls_over
    skip
    assert_equal "23:00", Clock.at(-25, 0).to_s
  end

  def test_negative_hour_rolls_over_continuously
    skip
    assert_equal "05:00", Clock.at(-91, 0).to_s
  end

  def test_negative_minutes
    skip
    assert_equal "00:20", Clock.at(1, -40).to_s
  end

  def test_negative_minutes_roll_over
    skip
    assert_equal "22:20", Clock.at(1, -160).to_s
  end

  def test_negative_minutes_roll_over_continuously
    skip
    assert_equal "16:40", Clock.at(1, -4820).to_s
  end

  def test_negative_hour_and_minutes_both_roll_over
    skip
    assert_equal "20:20", Clock.at(-25, -160).to_s
  end

  def test_negative_hour_and_minutes_both_roll_over_continuously
    skip
    assert_equal "22:10", Clock.at(-121, -5810).to_s
  end

  def test_add_minutes
    skip
    assert_equal "10:03", (Clock.at(10, 0) + 3).to_s
  end

  def test_add_no_minutes
    skip
    assert_equal "06:41", (Clock.at(6, 41) + 0).to_s
  end

  def test_add_to_next_hour
    skip
    assert_equal "01:25", (Clock.at(0, 45) + 40).to_s
  end

  def test_add_more_than_one_hour
    skip
    assert_equal "11:01", (Clock.at(10, 0) + 61).to_s
  end

  def test_add_more_than_two_hours_with_carry
    skip
    assert_equal "03:25", (Clock.at(0, 45) + 160).to_s
  end

  def test_add_across_midnight
    skip
    assert_equal "00:01", (Clock.at(23, 59) + 2).to_s
  end

  def test_add_more_than_one_day__1500_min_is_equal_to_25_hrs
    skip
    assert_equal "06:32", (Clock.at(5, 32) + 1500).to_s
  end

  def test_add_more_than_two_days
    skip
    assert_equal "11:21", (Clock.at(1, 1) + 3500).to_s
  end

  def test_subtract_minutes
    skip
    assert_equal "10:00", (Clock.at(10, 3) + -3).to_s
  end

  def test_subtract_to_previous_hour
    skip
    assert_equal "09:33", (Clock.at(10, 3) + -30).to_s
  end

  def test_subtract_more_than_an_hour
    skip
    assert_equal "08:53", (Clock.at(10, 3) + -70).to_s
  end

  def test_subtract_across_midnight
    skip
    assert_equal "23:59", (Clock.at(0, 3) + -4).to_s
  end

  def test_subtract_more_than_two_hours
    skip
    assert_equal "21:20", (Clock.at(0, 0) + -160).to_s
  end

  def test_subtract_more_than_two_hours_with_borrow
    skip
    assert_equal "03:35", (Clock.at(6, 15) + -160).to_s
  end

  def test_subtract_more_than_one_day__1500_min_is_equal_to_25_hrs
    skip
    assert_equal "04:32", (Clock.at(5, 32) + -1500).to_s
  end

  def test_subtract_more_than_two_days
    skip
    assert_equal "00:20", (Clock.at(2, 20) + -3000).to_s
  end

  def test_clocks_with_same_time
    skip
    clock1 = Clock.at(15, 37)
    clock2 = Clock.at(15, 37)
    assert clock1 == clock2
  end

  def test_clocks_a_minute_apart
    skip
    clock1 = Clock.at(15, 36)
    clock2 = Clock.at(15, 37)
    refute clock1 == clock2
  end

  def test_clocks_an_hour_apart
    skip
    clock1 = Clock.at(14, 37)
    clock2 = Clock.at(15, 37)
    refute clock1 == clock2
  end

  def test_clocks_with_hour_overflow
    skip
    clock1 = Clock.at(10, 37)
    clock2 = Clock.at(34, 37)
    assert clock1 == clock2
  end

  def test_clocks_with_hour_overflow_by_several_days
    skip
    clock1 = Clock.at(3, 11)
    clock2 = Clock.at(99, 11)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_hour
    skip
    clock1 = Clock.at(22, 40)
    clock2 = Clock.at(-2, 40)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_hour_that_wraps
    skip
    clock1 = Clock.at(17, 3)
    clock2 = Clock.at(-31, 3)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_hour_that_wraps_multiple_times
    skip
    clock1 = Clock.at(13, 49)
    clock2 = Clock.at(-83, 49)
    assert clock1 == clock2
  end

  def test_clocks_with_minute_overflow
    skip
    clock1 = Clock.at(0, 1)
    clock2 = Clock.at(0, 1441)
    assert clock1 == clock2
  end

  def test_clocks_with_minute_overflow_by_several_days
    skip
    clock1 = Clock.at(2, 2)
    clock2 = Clock.at(2, 4322)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_minute
    skip
    clock1 = Clock.at(2, 40)
    clock2 = Clock.at(3, -20)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_minute_that_wraps
    skip
    clock1 = Clock.at(4, 10)
    clock2 = Clock.at(5, -1490)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_minute_that_wraps_multiple_times
    skip
    clock1 = Clock.at(6, 15)
    clock2 = Clock.at(6, -4305)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_hours_and_minutes
    skip
    clock1 = Clock.at(7, 32)
    clock2 = Clock.at(-12, -268)
    assert clock1 == clock2
  end

  def test_clocks_with_negative_hours_and_minutes_that_wrap
    skip
    clock1 = Clock.at(18, 7)
    clock2 = Clock.at(-54, -11513)
    assert clock1 == clock2
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 2, BookKeeping::VERSION
  end
end
class Clock
  attr_reader :hour, :minute

  def initialize(hour, minute)
    @hour = hour
    @minute = minute

    handle_overflow
  end

  def self.at(hour, minute = 0)
    Clock.new(hour, minute)
  end

  def +(minutes)
    Clock.new(hour, minute + minutes)
  end

  def -(minutes)
    self + (-minutes)
  end

  def ==(other)
    hour == other.hour && minute == other.minute
  end

  def to_s
    sprintf("%02d:%02d", hour, minute)
  end

  private

  def handle_overflow
    extra_hours, @minute = minute.divmod(60)
    @hour = (hour + extra_hours) % 24
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of monkbroc

Once again, we came up with the same solution.

Avatar of monkbroc

Question for you: do you know how to do alias_method for a class method? I wanted to do alias_method :at, :new in my submission but that's only for instance methods.

Avatar of remcopeereboom

@monkbroc Aliasing a method only works on instance methods. By far the easiest way to get around this is to change the context of self to that of the class, so that the class methods are just instance methods: class << self # Change context to that of the class alias_method :at, :new end

An excellent solution by the way. I would never have considered using an alias for that!

Avatar of monkbroc

Thanks. I'm still wrapping my head around the details of the ruby ancestor chain. There are a lot of little subtleties.

Avatar of remcopeereboom

There are a lot, but be careful about depending on those subtleties. Even if you understand them, they'll trip some one else up for sure. There are a lot of truly excellent books about ruby out there, but I don't think there is a single one that describes all the nuances of classes and modules. "Metaprogramming Ruby" and "Practical Object Oriented Programming in Ruby" go a long way however, so you might want to check those out. In general I can only give the advice to stay focussed on self. Than you'll almost always figure out what is going on.

Avatar of monkbroc

I read Sandy Metz' book. It was interesting.

The kind of pitfall I'm talking about is like monkey patching a module that has already been included in another module.

For example, instead of monkey patching array to include accumulate in one exercise I wanted to monkey patch enumerable. But I wanted to keep my new method in its own module and include that into enumerable. I couldn't figure out exactly how to do that (include module into enumerable so that array responds to a new method). Monkey patching enumerable directly worked though.

Avatar of remcopeereboom

Hmm that is an interesting thought. This describes a basic situation: module B def foo "foo" end end

module A include B end

class C include A

end

p C.new.foo

This is roughly how ruby does method calls (I think):

Check singleton methods Check instance methods Check modules in the proper order (the order is what can bit you with this sort of thing) For each module check what other modules add to it. Check super class... Check super class modules Rinse and repeat.

Avatar of drewprice

In my solution, rather than creating an instance of Clock in the at method, I decided to create a new class, Clocknum, that would handle all of the other operations. My thinking was that this might model the relationship between the clock and it's value a bit better, as a clock only needs to know the time, not how to manipulate that time... but I'm not completely sold on that thought. The Clock class in my solution ends up feeling a bit superfluous (which may indicate that Clocknum itself is superfluous?).

The point is, I'm curious why you decided to wrap it all up together? What went into consideration while creating the class?

Avatar of remcopeereboom

@drewprice As any engineering discipline, there is always more than one way to Rome. I actually spent a bit of time thinking about what abstraction to use for the implementation. I considered having an underlying Ruby Time object handle all the operations and just moduloing that to hours and minutes; very similar to your Clocknum class. But when it came time to implement I decided to follow the golden rule of KISS: do the simplest thing first. And just having attributes for the hours and minutes seemed just a little simpler than instantiating a Time object (there is no simple constructor for just hours and minutes or even hours, minutes, and seconds). From there it was a pretty straightforward implementation and I never felt the need to refactor.

I don't see a lot of benefit of having an extra layer of abstraction in here. I can't see another use case for Clocknum, but it would depend on the code base (exercism isn't really good for architectural questions). I do think that there is a big chance that you might want to do conversions to and from Ruby's build-in time objects in which case that extra layer of abstraction can help greatly. You could just have a conversion from Time to minutes and hours and do all the operations in the build-in class. I wish I could add a diagram here...

If you feel that a class is superfluous, you could always remove it. Does your class still have a responsibility of its own? And most importantly, do clients care?

Avatar of drewprice

@remcopeereboom As always, thank you for taking the time. Your explanations have been hugely helpful in expanding the way I think about the code I'm writing.

Avatar of remcopeereboom

@drewprice Glad to hear it.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?