ordinal_word 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/ordinal_word.rb +102 -0
- data/tests/test_ordinal_word.rb +57 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c5e0de20394b739ece838666ed1bfdbb7020396e
|
4
|
+
data.tar.gz: c3466a622c4352f0ff98b548a13f4bb8fa7a8cbb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f12e13036a5eaef25010bece396d855c1a85cbfeab8884c6bbc8e9c608a1f00646176081d650ee6cd1793971a70e70c51c565098dd38e9f47433351005aa3ade
|
7
|
+
data.tar.gz: 37d3e6ec0a87780ec1aa3876d07f12da93d7e98dbe763633d0bdf334bd52556fa1e20fdc77e3ee6fa0be8dfa522ae1e05f76fa04f83a96664cac61bbb0b01f7b
|
data/lib/ordinal_word.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Current version accepts numbers only in range [1..99]
|
2
|
+
|
3
|
+
module OrdinalWord
|
4
|
+
|
5
|
+
class OrdinalWordError < Exception
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.wordinalize(int)
|
9
|
+
raise OrdinalWordError.new("Argument is not an Integer.") unless int.is_a?(Integer)
|
10
|
+
raise OrdinalWordError.new("Number is less than 1.") if int < 1
|
11
|
+
raise OrdinalWordError.new("Unfortunately, the module doesn't support numbers more than 99.") if int > 99
|
12
|
+
if @@ordinalize_hash.has_key?(int)
|
13
|
+
return @@ordinalize_hash[int]
|
14
|
+
else
|
15
|
+
tens = int / 10
|
16
|
+
ones = int % 10
|
17
|
+
return @@cardinal_hash[tens*10] + '-' + @@ordinalize_hash[ones]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
@@ordinalize_hash = {
|
24
|
+
1 => 'first',
|
25
|
+
2 => 'second',
|
26
|
+
3 => 'third',
|
27
|
+
4 => 'fourth',
|
28
|
+
5 => 'fifth',
|
29
|
+
6 => 'sixth',
|
30
|
+
7 => 'seventh',
|
31
|
+
8 => 'eighth',
|
32
|
+
9 => 'ninth',
|
33
|
+
10 => 'tenth',
|
34
|
+
11 => 'eleventh',
|
35
|
+
12 => 'twelfth',
|
36
|
+
13 => 'thirteenth',
|
37
|
+
14 => 'fourteenth',
|
38
|
+
15 => 'fifteenth',
|
39
|
+
16 => 'sixteenth',
|
40
|
+
17 => 'seventeenth',
|
41
|
+
18 => 'eighteenth',
|
42
|
+
19 => 'nineteenth',
|
43
|
+
20 => 'twentieth',
|
44
|
+
30 => 'thirtieth',
|
45
|
+
40 => 'fortieth',
|
46
|
+
50 => 'fiftieth',
|
47
|
+
60 => 'sixtieth',
|
48
|
+
70 => 'seventieth',
|
49
|
+
80 => 'eightieth',
|
50
|
+
90 => 'ninetieth',
|
51
|
+
=begin
|
52
|
+
100 => 'one hundredth',
|
53
|
+
1_000 => 'one thousandth',
|
54
|
+
1_000_000 => 'one millionth'
|
55
|
+
=end
|
56
|
+
}
|
57
|
+
|
58
|
+
@@cardinal_hash = {
|
59
|
+
1 => 'one',
|
60
|
+
2 => 'two',
|
61
|
+
3 => 'three',
|
62
|
+
4 => 'four',
|
63
|
+
5 => 'five',
|
64
|
+
6 => 'six',
|
65
|
+
7 => 'seven',
|
66
|
+
8 => 'eight',
|
67
|
+
9 => 'nine',
|
68
|
+
10 => 'ten',
|
69
|
+
11 => 'eleven',
|
70
|
+
12 => 'twelve',
|
71
|
+
13 => 'thirteen',
|
72
|
+
14 => 'fourteen',
|
73
|
+
15 => 'fifteen',
|
74
|
+
16 => 'sixteen',
|
75
|
+
17 => 'seventeen',
|
76
|
+
18 => 'eighteen',
|
77
|
+
19 => 'nineteen',
|
78
|
+
20 => 'twenty',
|
79
|
+
30 => 'thirty',
|
80
|
+
40 => 'forty',
|
81
|
+
50 => 'fifty',
|
82
|
+
60 => 'sixty',
|
83
|
+
70 => 'seventy',
|
84
|
+
80 => 'eighty',
|
85
|
+
90 => 'ninety',
|
86
|
+
=begin
|
87
|
+
100 => 'one hundred',
|
88
|
+
101 => 'a hundred and one',
|
89
|
+
110 => 'a hundred and ten',
|
90
|
+
120 => 'a hundred and twenty',
|
91
|
+
200 => 'two hundred',
|
92
|
+
1,000 => 'one thousand',
|
93
|
+
1,001 => 'a thousand and one',
|
94
|
+
1,010 => 'a thousand and ten',
|
95
|
+
100,000 => 'one hundred thousand',
|
96
|
+
1,000,000 => 'one million',
|
97
|
+
2,000,000 => 'two million'
|
98
|
+
=end
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "./lib/ordinal_word.rb"
|
2
|
+
require "minitest/autorun"
|
3
|
+
|
4
|
+
class OrdinalNameTest < MiniTest::Test
|
5
|
+
|
6
|
+
def test_should_raise_exception_if_not_integer
|
7
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize('df')}
|
8
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(2.2)}
|
9
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(Array.new)}
|
10
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize('Hash.new')}
|
11
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(nil)}
|
12
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(-1)}
|
13
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(0)}
|
14
|
+
assert_raises(OrdinalWord::OrdinalWordError) {OrdinalWord.wordinalize(100)}
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_properly_transform_numbers_from_1_to_99
|
18
|
+
assert_equal('first', OrdinalWord.wordinalize(1))
|
19
|
+
assert_equal('second', OrdinalWord.wordinalize(2))
|
20
|
+
assert_equal('third', OrdinalWord.wordinalize(3))
|
21
|
+
assert_equal('fourth', OrdinalWord.wordinalize(4))
|
22
|
+
assert_equal('fifth', OrdinalWord.wordinalize(5))
|
23
|
+
assert_equal('sixth', OrdinalWord.wordinalize(6))
|
24
|
+
assert_equal('seventh', OrdinalWord.wordinalize(7))
|
25
|
+
assert_equal('eighth', OrdinalWord.wordinalize(8))
|
26
|
+
assert_equal('ninth', OrdinalWord.wordinalize(9))
|
27
|
+
assert_equal('tenth', OrdinalWord.wordinalize(10))
|
28
|
+
assert_equal('eleventh', OrdinalWord.wordinalize(11))
|
29
|
+
assert_equal('twelfth', OrdinalWord.wordinalize(12))
|
30
|
+
assert_equal('thirteenth', OrdinalWord.wordinalize(13))
|
31
|
+
assert_equal('fourteenth', OrdinalWord.wordinalize(14))
|
32
|
+
assert_equal('fifteenth', OrdinalWord.wordinalize(15))
|
33
|
+
assert_equal('sixteenth', OrdinalWord.wordinalize(16))
|
34
|
+
assert_equal('seventeenth', OrdinalWord.wordinalize(17))
|
35
|
+
assert_equal('eighteenth', OrdinalWord.wordinalize(18))
|
36
|
+
assert_equal('nineteenth', OrdinalWord.wordinalize(19))
|
37
|
+
assert_equal('twentieth', OrdinalWord.wordinalize(20))
|
38
|
+
assert_equal('thirtieth', OrdinalWord.wordinalize(30))
|
39
|
+
assert_equal('fortieth', OrdinalWord.wordinalize(40))
|
40
|
+
assert_equal('fiftieth', OrdinalWord.wordinalize(50))
|
41
|
+
assert_equal('sixtieth', OrdinalWord.wordinalize(60))
|
42
|
+
assert_equal('seventieth', OrdinalWord.wordinalize(70))
|
43
|
+
assert_equal('eightieth', OrdinalWord.wordinalize(80))
|
44
|
+
assert_equal('ninetieth', OrdinalWord.wordinalize(90))
|
45
|
+
|
46
|
+
assert_equal('thirty-first', OrdinalWord.wordinalize(31))
|
47
|
+
assert_equal('forty-second', OrdinalWord.wordinalize(42))
|
48
|
+
assert_equal('fifty-third', OrdinalWord.wordinalize(53))
|
49
|
+
assert_equal('sixty-fourth', OrdinalWord.wordinalize(64))
|
50
|
+
assert_equal('seventy-fifth', OrdinalWord.wordinalize(75))
|
51
|
+
assert_equal('eighty-sixth', OrdinalWord.wordinalize(86))
|
52
|
+
assert_equal('ninety-seventh', OrdinalWord.wordinalize(97))
|
53
|
+
assert_equal('ninety-eighth', OrdinalWord.wordinalize(98))
|
54
|
+
assert_equal('ninety-ninth', OrdinalWord.wordinalize(99))
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ordinal_word
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexey Kharkin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ' Converts a number in range 1-99 (e.g. 1) to ordinal word (e.g. first)
|
14
|
+
in Ruby. '
|
15
|
+
email:
|
16
|
+
- alexey.kharkin@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/ordinal_word.rb
|
22
|
+
- tests/test_ordinal_word.rb
|
23
|
+
homepage:
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Converts a number in range 1-99 (e.g. 1) to ordinal word (e.g. first).
|
47
|
+
test_files:
|
48
|
+
- tests/test_ordinal_word.rb
|