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

remcopeereboom's solution

to Space Age in the Ruby Track

Published at Jul 13 2018 · 5 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.

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 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 space_age_test.rb

To include color from the command line:

ruby -r minitest/pride space_age_test.rb

Source

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.

space_age_test.rb

require 'minitest/autorun'
require_relative 'space_age'

# Common test data version: 1.0.0 7c63e40
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
  end

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

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

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

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

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

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

  def test_age_on_neptune
    skip
    age = SpaceAge.new(8_210_123_456)
    assert_in_delta 1.58, age.on_neptune, DELTA
  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 1, BookKeeping::VERSION
  end
end
class SpaceAge
  attr_reader :seconds

  def initialize(seconds)
    @seconds = seconds
  end

  {
    earth:   1.0,
    mercury: 0.2408467,
    venus:   0.61519726,
    mars:    1.8808158,
    jupiter: 11.862615,
    saturn:  29.447498,
    uranus:  84.016846,
    neptune: 164.79132
  }.each do |planet, earth_years|
    seconds_per_year = 60 * 60 * 24  * 365.25
    define_method :"on_#{planet}" do
      seconds.fdiv(seconds_per_year * earth_years).round(2)
    end
  end
end

Community comments

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

Cleaned up the code a little. Removed private because you could be mistaken that the methods generated were private which they're not. I had it there to show that the code is of no interest to clients of the class, but I think now that that might be the exact reason why the code should be in the public section.

Avatar of monkbroc

I can't help but think that the test for this one is setting a bad example for comparing floats with equality. Better to use assert_in_delta.

Avatar of remcopeereboom

I'll go an fix the tests and submit a pull request.

Avatar of monkbroc

I actually did sent in a PR right after I wrote this comment

You can comment there with your opinion.

Avatar of remcopeereboom

Yeah I noticed after my PR :)

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?