Avatar of w1zeman1p

w1zeman1p's solution

to Proverb in the Ruby Track

Published at Feb 17 2019 · 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
  attr_reader :chain
  attr_reader :qualifier

  def initialize(*chain, qualifier: "")
    @chain = chain
    @qualifier = qualifier
  end

  def to_s
    lines.join("\n")
  end

  private

  def last_line
    "And all for the want of a #{qualifier} #{chain.first}.".squeeze(" ")
  end

  def lines
    @lines ||= chain
      .each_cons(2)
      .map do |thing1, thing2|
        "For want of a #{thing1} the #{thing2} was lost."
      end
      .push(last_line)
  end
end

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?