๐ŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ๐ŸŽ‰
Avatar of katrinleinweber

katrinleinweber's solution

to Space Age in the Ruby Track

Published at Jan 10 2021 · 0 comments
Test suite

Given an age in seconds, calculate how old someone would be on:

  • Earth: orbital period 365.25 Earth days, or 31557600 seconds
  • Mercury: orbital period 0.2408467 Earth years
  • Venus: orbital period 0.61519726 Earth years
  • Mars: orbital period 1.8808158 Earth years
  • Jupiter: orbital period 11.862615 Earth years
  • Saturn: orbital period 29.447498 Earth years
  • Uranus: orbital period 84.016846 Earth years
  • Neptune: orbital period 164.79132 Earth years

So if you were told someone were 1,000,000,000 seconds old, you should be able to say that they're 31.69 Earth-years old.

If you're wondering why Pluto didn't make the cut, go watch this youtube video.

For installation and learning resources, refer to the Ruby resources 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 space_age_test.rb

To include color from the command line:

ruby -r minitest/pride space_age_test.rb


Partially inspired by Chapter 1 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=01

Submitting Incomplete Solutions

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


require 'minitest/autorun'
require_relative 'space_age'

# Common test data version: 1.1.0 8d4df79
class SpaceAgeTest < Minitest::Test
  # assert_in_delta will pass if the difference
  # between the values being compared is less
  # than the allowed delta
  DELTA = 0.01

  def test_age_on_earth
    # skip
    age = SpaceAge.new(1_000_000_000)
    assert_in_delta 31.69, age.on_earth, DELTA

  def test_age_on_mercury
    age = SpaceAge.new(2_134_835_688)
    assert_in_delta 280.88, age.on_mercury, DELTA

  def test_age_on_venus
    age = SpaceAge.new(189_839_836)
    assert_in_delta 9.78, age.on_venus, DELTA

  def test_age_on_mars
    age = SpaceAge.new(2_329_871_239)
    assert_in_delta 39.25, age.on_mars, DELTA

  def test_age_on_jupiter
    age = SpaceAge.new(901_876_382)
    assert_in_delta 2.41, age.on_jupiter, DELTA

  def test_age_on_saturn
    age = SpaceAge.new(3_000_000_000)
    assert_in_delta 3.23, age.on_saturn, DELTA

  def test_age_on_uranus
    age = SpaceAge.new(3_210_123_456)
    assert_in_delta 1.21, age.on_uranus, DELTA

  def test_age_on_neptune
    age = SpaceAge.new(8_210_123_456)
    assert_in_delta 1.58, age.on_neptune, DELTA
class SpaceAge
  attr_reader :seconds

  def initialize(seconds)
    @seconds = seconds

  SEC_PER_Y = {
    on_earth: (365.25 * 24 * 60 * 60),
    # Float value โ˜๏ธ of seconds per year,
    # and multiplied with relative
    # orbital periods ๐Ÿ‘‡
    on_neptune: 5200418560.0320,
    on_uranus:  2651370019.3296,
    on_saturn:   929292362.8848,
    on_jupiter:  374355659.1240,
    on_mars:      59354032.6901,
    on_venus:     19414149.0522,
    on_mercury:    7600543.8199,

  def method_missing(msg, *args)
    # Let Ruby find the corresponding conversion factor,
    # but guard against unknown messages / called methods.
    # Test for guard: assert_raises(NoMethodError) { || age.xyz }
    super(msg, *args) unless known_planet_in(msg)

    seconds / SEC_PER_Y[msg]

  private def known_planet_in(msg)

Community comments

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

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?