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

Cheese's solution

to Proverb in the Ruby Track

Published at Apr 06 2020 · 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

Source

Wikipedia http://en.wikipedia.org/wiki/For_Want_of_a_Nail

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
    def initialize(*words, qualifier:"")
      @words = words
      @qualifier = qualifier.empty? ? "" : qualifier.concat(" ")
    end
  
    def to_s
      @result = ""
      @words.each.with_index do |word, i|
        if i == @words.length-1
          and_all(@qualifier, @words[0])
        else
          for_want(@words[i], @words[i+1])
        end
      end
      @result
    end
  
    def for_want(word1, word2)
      @result += "For want of a #{word1} the #{word2} was lost.\n"
    end
  
    def and_all(qualifier, word)
      @result += "And all for the want of a #{qualifier}#{word}."
    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?
  • Are there new concepts here that you could read more about to improve your understanding?