Exercism Ruby: Gigasecond

Write a program that calculates the moment when someone has lived for 10^9 seconds.

A gigasecond is 10^9 (1,000,000,000) seconds.

My solution (with a lot of digging around Google..)

First, define Gigasecond as a Ruby constant since that shouldn’t ever change.

Then, to figure out the moment someone has lived for that long, take their birthdate and add the defined constant to it.  (No need to do any conversions as the date/time format for the result is no different from format of the given birthdate.)

screen-shot-2016-12-06-at-12-59-28-pm

Here are test cases it should pass.

Notes: Encountered .from for the first time in this problem.  Also learned to define Ruby constants once again.

To submit exercism problems: exercism submit path/to/file.rb

Advertisements
Exercism Ruby: Gigasecond

Grandfather Clock

Write a method which takes a block and calls it once for each hour that has passed today. That way, if I were to pass in the block do puts ‘DONG!‘ end, it would chime (sort of) like a grandfather clock. Test your method out with a few different blocks (including the one I just gave you). Hint: You can use Time.now.hour to get the current hour. However, this returns a number between 0 and 23, so you will have to alter those numbers in order to get ordinary clock-face numbers (1 to 12)..

screen-shot-2016-11-01-at-11-44-22-am

Why proc instead of method?

Grandfather Clock