dot_number 0.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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/dot_number.rb +31 -0
  3. data/lib/text_to_number.rb +60 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae9dc53cb240c2ecf4cd40019cd9ee5d9bb33217ee8128b552807994e2e347c6
4
+ data.tar.gz: a5cb3fb5769196f5c5d31226a9b8b5c4e8f5ae6b2ef14b145c793edee4a82400
5
+ SHA512:
6
+ metadata.gz: 7d52df9aa6ab1c3ba738c8bfce4eeddac8b0aa1105343fd786fa785787627d464f2c477379b71e134c728f39f37494b6776b26ca196813225554d81f49e8d77b
7
+ data.tar.gz: 11e352eb200e6d3a9df55d4bfe1fc3bc8f76cb4e58d83ab13d44c5db72fb1c9507a2e87ce06019e7cad534f618295658d5ad7d1c6e4327051acfa966ce18400d
data/lib/dot_number.rb ADDED
@@ -0,0 +1,31 @@
1
+ '''
2
+ Licensed under the Apache License, Version 2.0 (the "License");
3
+ you may not use this file except in compliance with the License.
4
+ You may obtain a copy of the License at
5
+
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ Unless required by applicable law or agreed to in writing, software
9
+ distributed under the License is distributed on an "AS IS" BASIS,
10
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ See the License for the specific language governing permissions and
12
+ limitations under the License.
13
+ '''
14
+
15
+ require_relative "text_to_number.rb"
16
+
17
+ class Numeric
18
+ def method_missing(method_name, *args)
19
+ name = method_name.to_s
20
+ if name.end_with? "?" then
21
+ begin
22
+ numeric_value = text_to_number(name[..-2])
23
+ return self == numeric_value
24
+ rescue ArgumentError
25
+ raise NoMethodError.new()
26
+ end
27
+ else
28
+ raise NoMethodError.new()
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ '''
2
+ Licensed under the Apache License, Version 2.0 (the "License");
3
+ you may not use this file except in compliance with the License.
4
+ You may obtain a copy of the License at
5
+
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+
8
+ Unless required by applicable law or agreed to in writing, software
9
+ distributed under the License is distributed on an "AS IS" BASIS,
10
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ See the License for the specific language governing permissions and
12
+ limitations under the License.
13
+ '''
14
+
15
+ def text_to_place_value(t)
16
+ placevalues = {
17
+ "thous" => 1000, # "thous" because "and" is removed
18
+ "million" => 10**6,
19
+ "billion" => 10**9,
20
+ "trillion" => 10**12,
21
+ "quadrillion" => 10**15,
22
+ "quintillion" => 10**18,
23
+ "sextillion" => 10**21,
24
+ "septillion" => 10**24,
25
+ "octillion" => 10**27,
26
+ "nonillion" => 10**30,
27
+ "decillion" => 10**33,
28
+ "" => 1
29
+ }
30
+ return 1 if t == nil
31
+ baseplace = t.start_with?("hundred") ? 100 : 1
32
+ t.gsub! "hundred",""
33
+ raise ArgumentError.new("Could not interpret \"#{t}\"") unless placevalues.has_key? t
34
+ baseplace * placevalues[t]
35
+ end
36
+
37
+
38
+ def text_to_number(t)
39
+ things_that_mean_negative = /minus|negative/
40
+ negative = t.scan(things_that_mean_negative).length.odd?
41
+
42
+ t.gsub! things_that_mean_negative,""
43
+ t.gsub! /and|_|-|\s/, ""
44
+
45
+ total = 0
46
+ segment(t).each do |seg|
47
+ total += seg[0].to_i * text_to_place_value(seg[1])
48
+ end
49
+ negative ? -total : total
50
+ end
51
+
52
+ def segment(t)
53
+ smallnumbers = {"ninetynine"=>99, "ninetyeight"=>98, "ninetyseven"=>97, "ninetysix"=>96, "ninetyfive"=>95, "ninetyfour"=>94, "ninetythree"=>93, "ninetytwo"=>92, "ninetyone"=>91, "ninety"=>90, "eightynine"=>89, "eightyeight"=>88, "eightyseven"=>87, "eightysix"=>86, "eightyfive"=>85, "eightyfour"=>84, "eightythree"=>83, "eightytwo"=>82, "eightyone"=>81, "eighty"=>80, "seventynine"=>79, "seventyeight"=>78, "seventyseven"=>77, "seventysix"=>76, "seventyfive"=>75, "seventyfour"=>74, "seventythree"=>73, "seventytwo"=>72, "seventyone"=>71, "seventy"=>70, "sixtynine"=>69, "sixtyeight"=>68, "sixtyseven"=>67, "sixtysix"=>66, "sixtyfive"=>65, "sixtyfour"=>64, "sixtythree"=>63, "sixtytwo"=>62, "sixtyone"=>61, "sixty"=>60, "fiftynine"=>59, "fiftyeight"=>58, "fiftyseven"=>57, "fiftysix"=>56, "fiftyfive"=>55, "fiftyfour"=>54, "fiftythree"=>53, "fiftytwo"=>52, "fiftyone"=>51, "fifty"=>50, "fortynine"=>49, "fortyeight"=>48, "fortyseven"=>47, "fortysix"=>46, "fortyfive"=>45, "fortyfour"=>44, "fortythree"=>43, "fortytwo"=>42, "fortyone"=>41, "forty"=>40, "thirtynine"=>39, "thirtyeight"=>38, "thirtyseven"=>37, "thirtysix"=>36, "thirtyfive"=>35, "thirtyfour"=>34, "thirtythree"=>33, "thirtytwo"=>32, "thirtyone"=>31, "thirty"=>30, "twentynine"=>29, "twentyeight"=>28, "twentyseven"=>27, "twentysix"=>26, "twentyfive"=>25, "twentyfour"=>24, "twentythree"=>23, "twentytwo"=>22, "twentyone"=>21, "twenty"=>20, "nineteen"=>19, "eighteen"=>18, "seventeen"=>17, "sixteen"=>16, "fifteen"=>15, "fourteen"=>14, "thirteen"=>13, "twelve"=>12, "eleven"=>11, "ten"=>10, "nine"=>9, "eight"=>8, "seven"=>7, "six"=>6, "five"=>5, "four"=>4, "three"=>3, "two"=>2, "one"=>1, "zero"=>0}
54
+ t_old = t.clone
55
+ smallnumbers.each do |key, value|
56
+ t.gsub! key,"&#{value.to_s}&"
57
+ end
58
+ raise ArgumentError.new("Not a number") if t_old == t
59
+ t.split("&")[1..].each_slice(2).map{|a,b| [a,b]}
60
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dot_number
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - bowstones
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-03-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Check for integer equality for any number using syntax like '.zero?'
14
+ email: git@thebowstones.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/dot_number.rb
20
+ - lib/text_to_number.rb
21
+ homepage: https://gitlab.com/bowstones/dot_number
22
+ licenses:
23
+ - Apache-2.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.4.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: ".number?"
44
+ test_files: []