ruby - proverb
This commit is contained in:
parent
c5e159ed19
commit
b54cd4cc6a
47
ruby/proverb/README.md
Normal file
47
ruby/proverb/README.md
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
# Proverb
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
```text
|
||||||
|
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](http://exercism.io/languages/ruby/resources).
|
||||||
|
|
||||||
|
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](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.
|
21
ruby/proverb/proverb.rb
Normal file
21
ruby/proverb/proverb.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Proverb class
|
||||||
|
class Proverb
|
||||||
|
def initialize(*args, qualifier: nil)
|
||||||
|
@inputs = args.to_a
|
||||||
|
@qualifier = "#{qualifier} " if qualifier
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
lines.join("\n") + "And all for the want of a #{@qualifier}#{@inputs[0]}."
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def lines
|
||||||
|
@inputs.map.with_index do |_, index|
|
||||||
|
next if index == @inputs.length - 1
|
||||||
|
|
||||||
|
"For want of a #{@inputs[index]} the #{@inputs[index + 1]} was lost."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
72
ruby/proverb/proverb_test.rb
Normal file
72
ruby/proverb/proverb_test.rb
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user