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

# megasua's solution

## to Say in the Ruby Track

Published at Jun 25 2020 · 0 comments
Instructions
Test suite
Solution

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. If you're on Linux or Windows, eSpeakNG may be available with the command `espeak`.

## 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 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 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.2.0 a0cee46
class SayTest < Minitest::Test
def test_zero
# skip
number = 0
assert_equal "zero", Say.new(number).in_english
end

def test_one
skip
number = 1
assert_equal "one", Say.new(number).in_english
end

def test_fourteen
skip
number = 14
assert_equal "fourteen", Say.new(number).in_english
end

def test_twenty
skip
number = 20
assert_equal "twenty", Say.new(number).in_english
end

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

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

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

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

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

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

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

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

def test_a_big_number
skip
number = 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(number).in_english
end

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

def test_numbers_above_999_999_999_999_are_out_of_range
skip
number = 1_000_000_000_000
assert_raises(ArgumentError) do
Say.new(number).in_english
end
end
end``````
``````=begin
Write your code for the 'Say' exercise in this file. Make the tests in
`say_test.rb` pass.

To get started with TDD, see the `README.md` file in your
`ruby/say` directory.
=end

class Say
def initialize
end

def in_english
end
end``````