🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # remcopeereboom's solution

## to Say in the Ruby Track

Published at Jul 13 2018 · 3 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a number from 0 to 999,999,999,999, spell out that number in English.

## Step 1

Handle the basic case of 0 through 99.

If the input to the program is `22`, then the output should be `'twenty-two'`.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

• 0
• 14
• 50
• 98
• -1
• 100

### Extension

If you're on a Mac, shell out to Mac OS X's `say` program to talk out loud.

## Step 2

Implement breaking a number up into chunks of thousands.

So `1234567890` should yield a list like 1, 234, 567, and 890, while the far simpler `1000` should yield just 1 and 0.

The program must also report any values that are out of range.

## Step 3

Now handle inserting the appropriate scale word between those chunks.

So `1234567890` should yield `'1 billion 234 million 567 thousand 890'`

The program must also report any values that are out of range. It's fine to stop at "trillion".

## Step 4

Put it all together to get nothing but plain English.

`12345` should give `twelve thousand three hundred forty-five`.

The program must also report any values that are out of range.

### Extensions

Use and (correctly) when spelling out the number in English:

• 14 becomes "fourteen".
• 100 becomes "one hundred".
• 120 becomes "one hundred and twenty".
• 1002 becomes "one thousand and two".
• 1323 becomes "one thousand three hundred and twenty-three".

For installation and learning resources, refer to the exercism help 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 say_test.rb
``````

To include color from the command line:

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

## Source

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

## Submitting Incomplete Solutions

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

### say_test.rb

``````require 'minitest/autorun'
require_relative 'say'

# Common test data version: 1.0.0 be403e1
class SayTest < Minitest::Test
def test_zero
# skip
question = 0
assert_equal('zero', Say.new(question).in_english)
end

def test_one
skip
question = 1
assert_equal('one', Say.new(question).in_english)
end

def test_fourteen
skip
question = 14
assert_equal('fourteen', Say.new(question).in_english)
end

def test_twenty
skip
question = 20
assert_equal('twenty', Say.new(question).in_english)
end

def test_twenty_two
skip
question = 22
assert_equal('twenty-two', Say.new(question).in_english)
end

def test_one_hundred
skip
question = 100
assert_equal('one hundred', Say.new(question).in_english)
end

def test_one_hundred_twenty_three
skip
question = 123
assert_equal('one hundred twenty-three', Say.new(question).in_english)
end

def test_one_thousand
skip
question = 1_000
assert_equal('one thousand', Say.new(question).in_english)
end

def test_one_thousand_two_hundred_thirty_four
skip
question = 1_234
assert_equal('one thousand two hundred thirty-four', Say.new(question).in_english)
end

def test_one_million
skip
question = 1_000_000
assert_equal('one million', Say.new(question).in_english)
end

def test_one_million_two_thousand_three_hundred_forty_five
skip
question = 1_002_345
assert_equal('one million two thousand three hundred forty-five', Say.new(question).in_english)
end

def test_one_billion
skip
question = 1_000_000_000
assert_equal('one billion', Say.new(question).in_english)
end

def test_a_big_number
skip
question = 987_654_321_123
assert_equal('nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three', Say.new(question).in_english)
end

def test_numbers_below_zero_are_out_of_range
skip
question = -1
assert_raises ArgumentError do
Say.new(question).in_english
end
end

def test_numbers_above_999999999999_are_out_of_range
skip
question = 1_000_000_000_000
assert_raises ArgumentError do
Say.new(question).in_english
end
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
#   VERSION = 1 # Where the version number matches the one in the test.
# end
#
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end``````
``````class Say

ONES = {
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
}

TENS = {
20 => 'twenty',
30 => 'thirty',
40 => 'forty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninty',
}

def initialize(number)
@number = number
end

def in_english
to_word(number)
end

private

def to_word(n)
case n
when 0 then handle_zero(n)
when 1...16 then handle_ones(n)
when 16...20 then handle_tens(n)
when 20...100 then handle_teens(n)
when 100...1_000 then handle_hundreds(n)
when 1_000...1_000_000 then handle_thousands(n)
when 1_000_000...1_000_000_000 then handle_millions(n)
when 1_000_000_000...1_000_000_000_000 then handle_billions(n)
else fail ArgumentError, "ArgumentError #{n} for (0...1_000_000_000_000_000)"
end
end

def handle_zero(n)
'zero'
end

def handle_ones(n)
ONES[n]
end

def handle_tens(n)
ONES[n - 10] + 'teen'
end

def handle_teens(n)
factor, remainder = n.divmod(10)
TENS[factor * 10] + (remainder.zero? ? '' : "-#{ONES[remainder]}")
end

def handle_hundreds(n)
factor, remainder = n.divmod(100)
to_word(factor) + ' hundred' + (remainder.zero? ? '' : " #{to_word(remainder)}")
end

def handle_thousands(n)
factor, remainder = n.divmod(1000)
to_word(factor) + ' thousand' + (remainder.zero? ? '' : " #{to_word(remainder)}")
end

def handle_millions(n)
factor, remainder = n.divmod(1_000_000)
to_word(factor) + ' million' + (remainder.zero? ? '' : " #{to_word(remainder)}")
end

def handle_billions(n)
factor, remainder = n.divmod(1_000_000_000)
to_word(factor) + ' billion' + (remainder.zero? ? '' : " #{to_word(remainder)}")
end
end`````` Solution Author
commented over 5 years ago

Looooooots of duplication. I started out using an approach similar to with the Roman numerals. The tens are an annoying special case though, so I just went for simple to make the cases work first. will update later....

Little note.... The argument error should probably be in the constructor!!!! Apart from the duplication, this implementation is clear and easy to follow! Solution Author
commented over 5 years ago

Thanks, but I should at least combine the methods for numbers greater than 100. There are some pretty DRY solutions out there, but it is hard to keep a readable solution. Any suggestions are welcome.

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