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.)


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

Exercism Ruby: Gigasecond

Morse Code

Build a function, ‘morse_encode(str)’ that takes in a string (no numbers or punctuation) and outputs the morse code for it. See http://en.wikipedia.org/wiki/Morse_code. Put two spaces between words and one space between letters.

You’ll have to type in morse code: I’d use a hash to map letters to codes. Don’t worry about numbers.

My solution:

I learned the difference between global variables and Ruby constants while solving this problem.  They both serve similar purposes but global variables’ values can be changed while Ruby constants cannot.  Also, constants are written in all uppercase and global variables start with “$”.

Another thing to remember is how to retrieve key/value from hash.  Here’s a handy cheat sheet of shortcuts for Ruby hashes.


Test Cases: 


Morse Code