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

# ruby123's solution

## to Proverb in the Ruby Track

Published at Nov 13 2018 · 0 comments
Instructions
Test suite
Solution

For want of a horseshoe nail, a kingdom was lost, or so the saying goes.

Given a list of inputs, generate the relevant proverb. For example, given the list ["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"], you will output the full text of this proverbial rhyme:

For want of a nail the shoe was lost.
For want of a shoe the horse was lost.
For want of a horse the rider was lost.
For want of a rider the message was lost.
For want of a message the battle was lost.
For want of a battle the kingdom was lost.
And all for the want of a nail.

Note that the list of inputs may vary; your solution should be able to handle lists of arbitrary length and content. No line of the output text should be a static, unchanging string; all should vary according to the input given.

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

To include color from the command line:

ruby -r minitest/pride proverb_test.rb

## Submitting Incomplete Solutions

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

### proverb_test.rb

require 'minitest/autorun'
require_relative 'proverb'

class ProverbTest < Minitest::Test
def test_a_single_consequence
proverb = Proverb.new('nail', 'shoe')
expected = "For want of a nail the shoe was lost.\n" \
'And all for the want of a nail.'
assert_equal expected, proverb.to_s
end

def test_a_short_chain_of_consequences
skip
proverb = Proverb.new('nail', 'shoe', 'horse')
expected = "For want of a nail the shoe was lost.\n" \
"For want of a shoe the horse was lost.\n" \
'And all for the want of a nail.'
assert_equal expected, proverb.to_s
end

def test_a_longer_chain_of_consequences
skip
proverb = Proverb.new('nail', 'shoe', 'horse', 'rider')
expected = "For want of a nail the shoe was lost.\n" \
"For want of a shoe the horse was lost.\n" \
"For want of a horse the rider was lost.\n" \
'And all for the want of a nail.'
assert_equal expected, proverb.to_s
end

def test_proverb_does_not_hard_code_the_rhyme_dictionary
skip
proverb = Proverb.new('key', 'value')
expected = "For want of a key the value was lost.\n" \
'And all for the want of a key.'
assert_equal expected, proverb.to_s
end

def test_the_whole_proverb
skip
chain = %w(nail shoe horse rider message battle kingdom)
proverb = Proverb.new(*chain)
expected = "For want of a nail the shoe was lost.\n" \
"For want of a shoe the horse was lost.\n" \
"For want of a horse the rider was lost.\n" \
"For want of a rider the message was lost.\n" \
"For want of a message the battle was lost.\n" \
"For want of a battle the kingdom was lost.\n" \
'And all for the want of a nail.'
assert_equal expected, proverb.to_s
end

def test_an_optional_qualifier_in_the_final_consequence
skip
chain = %w(nail shoe horse rider message battle kingdom)
proverb = Proverb.new(*chain, qualifier: 'horseshoe')
expected = "For want of a nail the shoe was lost.\n" \
"For want of a shoe the horse was lost.\n" \
"For want of a horse the rider was lost.\n" \
"For want of a rider the message was lost.\n" \
"For want of a message the battle was lost.\n" \
"For want of a battle the kingdom was lost.\n" \
'And all for the want of a horseshoe nail.'
assert_equal expected, proverb.to_s
end

def test_proverb_is_same_each_time
skip
proverb = Proverb.new('nail', 'shoe')
assert_equal proverb.to_s, proverb.to_s
end
end
class Proverb

attr_accessor :items, :qualifier

def initialize(*items, qualifier: nil)
@items = items
@qualifier = qualifier
end

def to_s
output = ''
items_count = items.length
items.each_with_index do |item, i|
if i + 1 == items_count
qual = @qualifier ? "#{@qualifier} " : ''
output += "And all for the want of a #{qual}#{items[0]}."
else
output += "For want of a #{items[i]} the #{items[i + 1]} was lost.\n"
end
end
output
end
end