# cjavdev's solution

## to Twelve Days in the Ruby Track

Published at Feb 27 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Output the lyrics to 'The Twelve Days of Christmas'.

``````On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.

On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.

On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.

On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree.
``````

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 twelve_days_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride twelve_days_test.rb
``````

## Submitting Incomplete Solutions

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

### twelve_days_test.rb

``````require 'minitest/autorun'
require_relative 'twelve_days'

class TwelveDaysTest < Minitest::Test
# This test is an acceptance test.
#
# If you find it difficult to work the problem with so much
# to make sure you got it right.
# There's no need to submit the tests you write, unless you
# specifically want feedback on them.
def test_the_whole_song
song_file = File.expand_path('../song.txt', __FILE__)
assert_equal expected, TwelveDays.song
end
end``````
``````class TwelveDays
ORDINALS = [
nil,
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth"
].freeze
nil,
"a Partridge in a Pear Tree",
"two Turtle Doves",
"three French Hens",
"four Calling Birds",
"five Gold Rings",
"six Geese-a-Laying",
"seven Swans-a-Swimming",
"eight Maids-a-Milking",
"ten Lords-a-Leaping",
"eleven Pipers Piping",
"twelve Drummers Drumming",
].freeze

def self.song
verses = (1..12).map do |day_number|
new(day_number)
end

"#{verses.join("\n\n")}\n"
end

def initialize(day)
@day = day
end

def to_s
"On the #{nth_day} day of Christmas my true love gave to me: #{english_joined(gifts)}."
end

private

def nth_day
ORDINALS[day]
end

end

def english_joined(items)
return items.first if items.length == 1

"#{items[0...-1].join(", ")}, and #{items.last}"
end
end``````

### 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?