-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_lang.rb
72 lines (54 loc) · 1.55 KB
/
to_lang.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env ruby -w
# to_lang.rb
=begin rdoc
Implement representation of numbers in human languages:
1 => 'one',
2 => 'two',
etc.
This is an generalized extension of ideas shown for the
specific case of roman numerals in roman_numeral.rb
Note that similar work has already been done at
http://www.deveiate.org/projects/Linguistics/wiki/English
This version focuses only on converting numbers to multiple
language targets, and pedantically considers "and" to be
the pronunciation of the decimal point.
=end
class Integer
require 'representable_in_english'
require 'representable_in_spanish'
include Representable_In_English
include Representable_In_Spanish
EMPTY_STRING = ''
SPACE = ' '
@@lang_of ||= Hash.new()
def need_ones?(lang)
send("need_ones_in_#{lang}").keys.include?(self)
end
def to_lang(lang)
return EMPTY_STRING if self.zero?
@@lang_of[lang] ||= send("create_#{lang}")
base = get_base(lang)
mult = (self / base).to_i
remaining = (self - (mult * base))
raw_output = [
mult_prefix(base, mult, lang),
@@lang_of[lang][base],
remaining.to_lang(lang)
].join(SPACE)
return send(
"special_replacements_in_#{lang}",
raw_output)
end
private
def get_base(lang)
return self if @@lang_of[lang][self]
@@lang_of[lang].keys.sort.reverse.detect do |k|
k <= self
end
end
def mult_prefix(base, mult, lang)
return mult.to_lang(lang) if mult > 1
return 1.to_lang(lang) if base.need_ones?(lang)
return EMPTY_STRING
end
end