epitools 0.4.19 → 0.4.20

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,7 +12,7 @@ Extras:
12
12
  * {Rash}[http://rdoc.info/github/epitron/epitools/master/Rash] (a hash which can have Regexps as keys, allowing a single (key,value) pair to match many keys.)
13
13
  * {Progressbar}[http://rdoc.info/github/epitron/epitools/master/Progressbar] (better than the progressbar gem)
14
14
  * {Browser}[http://rdoc.info/github/epitron/epitools/master/Browser] (a fake browser, using mechanize, Progressbar, and CacheDB)
15
-
15
+
16
16
  == Installing
17
17
 
18
18
  gem install epitools
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.19
1
+ 0.4.20
data/epitools.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{epitools}
8
- s.version = "0.4.19"
8
+ s.version = "0.4.20"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["epitron"]
12
- s.date = %q{2011-03-30}
12
+ s.date = %q{2011-04-02}
13
13
  s.description = %q{Miscellaneous utility libraries to make my life easier.}
14
14
  s.email = %q{chris@ill-logic.com}
15
15
  s.extra_rdoc_files = [
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "lib/epitools/lcs.rb",
35
35
  "lib/epitools/metaclass.rb",
36
36
  "lib/epitools/niceprint.rb",
37
+ "lib/epitools/numwords.rb",
37
38
  "lib/epitools/path.rb",
38
39
  "lib/epitools/permutations.rb",
39
40
  "lib/epitools/pretty_backtrace.rb",
@@ -49,6 +50,7 @@ Gem::Specification.new do |s|
49
50
  "spec/clitools_spec.rb",
50
51
  "spec/lcs_spec.rb",
51
52
  "spec/metaclass_spec.rb",
53
+ "spec/numwords_spec.rb",
52
54
  "spec/path_spec.rb",
53
55
  "spec/permutations_spec.rb",
54
56
  "spec/rash_spec.rb",
@@ -69,6 +71,7 @@ Gem::Specification.new do |s|
69
71
  "spec/clitools_spec.rb",
70
72
  "spec/lcs_spec.rb",
71
73
  "spec/metaclass_spec.rb",
74
+ "spec/numwords_spec.rb",
72
75
  "spec/path_spec.rb",
73
76
  "spec/permutations_spec.rb",
74
77
  "spec/rash_spec.rb",
data/lib/epitools.rb CHANGED
@@ -34,6 +34,7 @@ end
34
34
  colored
35
35
  clitools
36
36
  permutations
37
+ numwords
37
38
  ].each do |mod|
38
39
  require_wrapper.call mod
39
40
  end
@@ -0,0 +1,110 @@
1
+ require 'epitools/basetypes'
2
+
3
+ class Numeric
4
+
5
+ NAMES_SMALL = [
6
+ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
7
+ ]
8
+
9
+ NAMES_MEDIUM = [
10
+ "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"
11
+ ]
12
+
13
+ NAMES_LARGE = [
14
+ "thousand", "million", "billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion", "octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion", "quattuordecillion", "quindecillion", "sexdecillion", "septendecillion", "octodecillion", "novemdecillion", "vigintillion", "unvigintillion", "duovigintillion", "trevigintillion", "quattuorvigintillion", "quinvigintillion", "sexvigintillion", "septenvigintillion", "octovigintillion", "novemvigintillion", "trigintillion", "untrigintillion", "duotrigintillion"
15
+ ]
16
+
17
+
18
+ #
19
+ # Convert this number to words (eg: 69 => 'sixty-nine').
20
+ # Works with numbers up to a googol (10^100).
21
+ #
22
+ def to_words
23
+
24
+ if is_a? Integer
25
+ num = self
26
+ else
27
+ num = self.to_i
28
+ end
29
+
30
+ if (n = to_s.size) > 102
31
+ return "more than a googol! (#{n} digits)"
32
+ end
33
+
34
+ whole_thing = []
35
+
36
+ triplets = commatize.split(',')
37
+ num_triplets = triplets.size
38
+
39
+ triplets.each_with_index do |triplet, i|
40
+ result = []
41
+
42
+ tens, hunds = nil, nil
43
+
44
+ chars = triplet.chars.to_a
45
+
46
+ raise "Error: Not a triplet: #{triplet}" if chars.size > 3 or chars.size < 1
47
+
48
+ if chars.size == 3
49
+ n = chars.shift.to_i
50
+ hunds = NAMES_SMALL[n] + "-hundred" if n > 0
51
+ chars.shift if chars.first == '0'
52
+ end
53
+
54
+ if chars.size == 2
55
+ n = chars.join('').to_i
56
+
57
+ if n > 0 and n < 20
58
+ tens = NAMES_SMALL[n]
59
+ elsif n > 0
60
+ tens = NAMES_MEDIUM[chars.shift.to_i - 2]
61
+ if chars.first != '0'
62
+ tens += "-" + NAMES_SMALL[chars.shift.to_i]
63
+ else
64
+ chars.shift
65
+ end
66
+ end
67
+ end
68
+
69
+ if chars.size == 1
70
+ n = chars.join('').to_i
71
+ tens = NAMES_SMALL[n] if n > 0
72
+ end
73
+
74
+
75
+ if hunds
76
+ if tens
77
+ result << "#{hunds} and #{tens}"
78
+ else
79
+ result << hunds
80
+ end
81
+ else
82
+ result << tens if tens
83
+ end
84
+
85
+ magnitude = (num_triplets - i)
86
+ result << NAMES_LARGE[magnitude-2] if magnitude > 1
87
+
88
+ whole_thing << result.join(' ')
89
+ end
90
+
91
+ whole_thing.join ', '
92
+
93
+ end
94
+
95
+ #
96
+ # Gives all numbers ".thousand", ".million", up to ".duotrigintillion" methods.
97
+ # eg: 10.million #=> 10_000_000
98
+ #
99
+ def method_missing(meth, &block)
100
+ if magnitude = NAMES_LARGE.index(meth.to_s)
101
+ pow = (magnitude+1) * 3
102
+ self * 10**pow
103
+ else
104
+ super
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+
data/lib/epitools/sys.rb CHANGED
File without changes
@@ -0,0 +1,29 @@
1
+ require 'epitools/numwords'
2
+
3
+ describe Numeric do
4
+
5
+ it "to_wordses" do
6
+ {
7
+ 10 => "ten",
8
+ 3_123 => "three thousand, one-hundred and twenty-three",
9
+ 123_124 => "one-hundred and twenty-three thousand, one-hundred and twenty-four",
10
+
11
+ 8_128_937_981_273_987_129_837_174_612_897_638_613 => "eight undecillion, one-hundred and twenty-eight decillion, nine-hundred and thirty-seven nonillion, nine-hundred and eighty-one octillion, two-hundred and seventy-three septillion, nine-hundred and eighty-seven sextillion, one-hundred and twenty-nine quintillion, eight-hundred and thirty-seven quadrillion, one-hundred and seventy-four trillion, six-hundred and twelve billion, eight-hundred and ninety-seven million, six-hundred and thirty-eight thousand, six-hundred and thirteen",
12
+
13
+ 3_486_597_230_495_871_304_981_320_498_123_498_263_984_739_841_834_091_823_094_812_039_481_231_623_987_461_293_874_698_123_649_817_236 => "three duotrigintillion, four-hundred and eighty-six untrigintillion, five-hundred and ninety-seven trigintillion, two-hundred and thirty novemvigintillion, four-hundred and ninety-five octovigintillion, eight-hundred and seventy-one septenvigintillion, three-hundred and four sexvigintillion, nine-hundred and eighty-one quinvigintillion, three-hundred and twenty quattuorvigintillion, four-hundred and ninety-eight trevigintillion, one-hundred and twenty-three duovigintillion, four-hundred and ninety-eight unvigintillion, two-hundred and sixty-three vigintillion, nine-hundred and eighty-four novemdecillion, seven-hundred and thirty-nine octodecillion, eight-hundred and fourty-one septendecillion, eight-hundred and thirty-four sexdecillion, ninety-one quindecillion, eight-hundred and twenty-three quattuordecillion, ninety-four tredecillion, eight-hundred and twelve duodecillion, thirty-nine undecillion, four-hundred and eighty-one decillion, two-hundred and thirty-one nonillion, six-hundred and twenty-three octillion, nine-hundred and eighty-seven septillion, four-hundred and sixty-one sextillion, two-hundred and ninety-three quintillion, eight-hundred and seventy-four quadrillion, six-hundred and ninety-eight trillion, one-hundred and twenty-three billion, six-hundred and fourty-nine million, eight-hundred and seventeen thousand, two-hundred and thirty-six",
14
+
15
+ 1763241823498172490817349807213409238409123409128340981234781236487126348791263847961238794612839468917236489712364987162398746129834698172364987123 => "more than a googol! (148 digits)"
16
+ }.each do |num, result|
17
+ num.to_words.should == result
18
+ end
19
+ end
20
+
21
+ it "has .thousand, .million, etc." do
22
+ 10.thousand.should == 10_000
23
+ 20.billion.should == 20_000_000_000
24
+ 1.9.million.should == 1_900_000
25
+ 37.2872.duotrigintillion.should == 37287200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
26
+ lambda { 10.googol }.should raise_error
27
+ end
28
+
29
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epitools
3
3
  version: !ruby/object:Gem::Version
4
- hash: 41
4
+ hash: 39
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 19
10
- version: 0.4.19
9
+ - 20
10
+ version: 0.4.20
11
11
  platform: ruby
12
12
  authors:
13
13
  - epitron
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-30 00:00:00 -04:00
18
+ date: 2011-04-02 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -91,6 +91,7 @@ files:
91
91
  - lib/epitools/lcs.rb
92
92
  - lib/epitools/metaclass.rb
93
93
  - lib/epitools/niceprint.rb
94
+ - lib/epitools/numwords.rb
94
95
  - lib/epitools/path.rb
95
96
  - lib/epitools/permutations.rb
96
97
  - lib/epitools/pretty_backtrace.rb
@@ -106,6 +107,7 @@ files:
106
107
  - spec/clitools_spec.rb
107
108
  - spec/lcs_spec.rb
108
109
  - spec/metaclass_spec.rb
110
+ - spec/numwords_spec.rb
109
111
  - spec/path_spec.rb
110
112
  - spec/permutations_spec.rb
111
113
  - spec/rash_spec.rb
@@ -154,6 +156,7 @@ test_files:
154
156
  - spec/clitools_spec.rb
155
157
  - spec/lcs_spec.rb
156
158
  - spec/metaclass_spec.rb
159
+ - spec/numwords_spec.rb
157
160
  - spec/path_spec.rb
158
161
  - spec/permutations_spec.rb
159
162
  - spec/rash_spec.rb