numeric 0.1.0 → 0.2.0
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/README.md +19 -4
- data/VERSION +1 -1
- data/lib/numeric.rb +89 -1
- data/numeric.gemspec +3 -3
- data/test/complex_test.rb +34 -0
- data/test/decimal_test.rb +36 -0
- data/test/float_test.rb +32 -0
- data/test/integer_test.rb +32 -0
- data/test/numeric_test.rb +1 -6
- data/test/rational_test.rb +32 -0
- metadata +30 -32
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a5648f543fe30ef5dd2acd36f648d8aeeda3619d
|
4
|
+
data.tar.gz: 95588bbf89c41a2ef19811509003a9a3b53e6fc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ac36642dc6fbc071d9b6c968900a8cbe5ec919bc8d61ba655628ff2435090b3f6652d471143d2ddf85b1cb89f1da0007d9798b726b8ca47a22661eae01f9bdf
|
7
|
+
data.tar.gz: fcc559e1bd4f6a0a6f0b152c69fb89063c28608204aeb7a97a51daa7b6c3990d6544f9c67ddf53cd5d269482f2be32973e6b07aa36bbc32d491ba52dda1eea3f
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Numeric
|
1
|
+
# Numeric [](https://travis-ci.org/dwbutler/numeric)
|
2
2
|
|
3
3
|
Checking if a given value is "numeric" is a common problem. How can we do in this Ruby?
|
4
4
|
There are many problematic ways, such as converting to an integer or float or using a regex:
|
@@ -24,11 +24,26 @@ The rules are simple:
|
|
24
24
|
* All strings or symbols that can be converted with `Complex()` are numeric.
|
25
25
|
* All other objects are not numeric.
|
26
26
|
|
27
|
+
If you want to get more specific, the following methods are also defined:
|
28
|
+
|
29
|
+
* `#integer?`
|
30
|
+
* `#float?`
|
31
|
+
* `#decimal?`
|
32
|
+
* `#rational?`
|
33
|
+
* `#complex?`
|
34
|
+
|
35
|
+
Note that for these more specific methods, a string that can be converted to
|
36
|
+
a `Float` (such as '1') will return `true` for `#float?`. An instance of a different
|
37
|
+
Numeric class (such as 1, Integer) will return `false` for `#float?`.
|
38
|
+
|
27
39
|
## Compatibility
|
28
40
|
|
29
|
-
The gem is tested to work in MRI Ruby 1.9
|
30
|
-
|
31
|
-
|
41
|
+
The gem is tested to work in MRI Ruby 1.9 - 2.0, and JRuby (1.9 mode).
|
42
|
+
All other Rubies are too lax in what their `Numeric` classes accept, which prevents
|
43
|
+
this gem from working completely.
|
44
|
+
|
45
|
+
MRI 2.0.0's `BigDecimal` class is also too lax in what it accepts, so `'a'.decimal?` will
|
46
|
+
unfortunately return `true`.
|
32
47
|
|
33
48
|
## Installation
|
34
49
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/numeric.rb
CHANGED
@@ -1,13 +1,57 @@
|
|
1
|
+
require 'rational'
|
2
|
+
require 'bigdecimal'
|
3
|
+
require 'complex'
|
4
|
+
|
1
5
|
class String
|
2
6
|
def numeric?
|
7
|
+
complex?
|
8
|
+
end
|
9
|
+
|
10
|
+
def complex?
|
3
11
|
!!Complex(self) rescue false
|
4
12
|
end
|
13
|
+
|
14
|
+
def rational?
|
15
|
+
!!Rational(self) rescue false
|
16
|
+
end
|
17
|
+
|
18
|
+
def integer?
|
19
|
+
!!Integer(self) rescue false
|
20
|
+
end
|
21
|
+
|
22
|
+
def float?
|
23
|
+
!!Float(self) rescue false
|
24
|
+
end
|
25
|
+
|
26
|
+
def decimal?
|
27
|
+
!!BigDecimal(self) rescue false
|
28
|
+
end
|
5
29
|
end
|
6
30
|
|
7
31
|
class Symbol
|
8
32
|
def numeric?
|
9
33
|
to_s.numeric?
|
10
34
|
end
|
35
|
+
|
36
|
+
def rational?
|
37
|
+
to_s.rational?
|
38
|
+
end
|
39
|
+
|
40
|
+
def complex?
|
41
|
+
to_s.complex?
|
42
|
+
end
|
43
|
+
|
44
|
+
def integer?
|
45
|
+
to_s.complex?
|
46
|
+
end
|
47
|
+
|
48
|
+
def float?
|
49
|
+
to_s.float?
|
50
|
+
end
|
51
|
+
|
52
|
+
def decimal?
|
53
|
+
to_s.decimal?
|
54
|
+
end
|
11
55
|
end
|
12
56
|
|
13
57
|
class Numeric
|
@@ -16,8 +60,52 @@ class Numeric
|
|
16
60
|
end
|
17
61
|
end
|
18
62
|
|
19
|
-
class
|
63
|
+
class Complex
|
64
|
+
def complex?
|
65
|
+
true
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Float
|
70
|
+
def float?
|
71
|
+
true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class Rational
|
76
|
+
def rational?
|
77
|
+
true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class BigDecimal
|
82
|
+
def decimal?
|
83
|
+
true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module Kernel
|
20
88
|
def numeric?
|
21
89
|
false
|
22
90
|
end
|
91
|
+
|
92
|
+
def complex?
|
93
|
+
false
|
94
|
+
end
|
95
|
+
|
96
|
+
def rational?
|
97
|
+
false
|
98
|
+
end
|
99
|
+
|
100
|
+
def integer?
|
101
|
+
false
|
102
|
+
end
|
103
|
+
|
104
|
+
def float?
|
105
|
+
false
|
106
|
+
end
|
107
|
+
|
108
|
+
def decimal?
|
109
|
+
false
|
110
|
+
end
|
23
111
|
end
|
data/numeric.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = version
|
9
9
|
spec.authors = ["David Butler"]
|
10
10
|
spec.email = ["dwbutler@ucla.edu"]
|
11
|
-
spec.description = %q{Adds
|
12
|
-
spec.summary = %q{Adds
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.description = %q{Adds numeric checking to Ruby}
|
12
|
+
spec.summary = %q{Adds numeric checking to Ruby}
|
13
|
+
spec.homepage = "https://github.com/dwbutler/numeric"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ComplexTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".complex?
|
6
|
+
assert "-1".complex?
|
7
|
+
assert "1.0".complex?
|
8
|
+
assert "j".complex?
|
9
|
+
assert "0.3-0.5i".complex?
|
10
|
+
|
11
|
+
assert !"a".complex?
|
12
|
+
assert !"1a".complex?
|
13
|
+
assert !"a1".complex?
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_symbol
|
17
|
+
assert :'0'.complex?
|
18
|
+
|
19
|
+
assert !:a.complex?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_object
|
23
|
+
assert !Object.new.complex?
|
24
|
+
assert !{}.complex?
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_numeric
|
28
|
+
assert !1.complex?
|
29
|
+
assert !Rational(1,2).complex?
|
30
|
+
assert Complex(1, 1).complex?
|
31
|
+
assert !2.3.complex?
|
32
|
+
assert !BigDecimal('1').complex?
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DecimalTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".decimal?
|
6
|
+
assert "-1".decimal?
|
7
|
+
assert "1.0".decimal?
|
8
|
+
assert "1e1".decimal?
|
9
|
+
assert "1/2".decimal?
|
10
|
+
assert "0.3-0.5i".decimal?
|
11
|
+
|
12
|
+
# MRI's BigDecimal is too relaxed in what it accepts
|
13
|
+
#pending !"a".decimal?
|
14
|
+
#pending !"1a".decimal?
|
15
|
+
#pending !"a1".decimal?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_symbol
|
19
|
+
assert :'0'.decimal?
|
20
|
+
|
21
|
+
#pend !:a.decimal?
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_object
|
25
|
+
assert !Object.new.decimal?
|
26
|
+
assert !{}.decimal?
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_numeric
|
30
|
+
assert !1.decimal?
|
31
|
+
assert !Rational(1,2).decimal?
|
32
|
+
assert !Complex(1, 1).decimal?
|
33
|
+
assert !2.3.decimal?
|
34
|
+
assert BigDecimal('1').decimal?
|
35
|
+
end
|
36
|
+
end
|
data/test/float_test.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FloatTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".float?
|
6
|
+
assert "-1".float?
|
7
|
+
assert "1.0".float?
|
8
|
+
|
9
|
+
assert !"a".float?
|
10
|
+
assert !"1a".float?
|
11
|
+
assert !"a1".float?
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_symbol
|
15
|
+
assert :'0'.float?
|
16
|
+
|
17
|
+
assert !:a.float?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_object
|
21
|
+
assert !Object.new.float?
|
22
|
+
assert !{}.float?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_numeric
|
26
|
+
assert !1.float?
|
27
|
+
assert !Rational(1,2).float?
|
28
|
+
assert !Complex(1, 1).float?
|
29
|
+
assert 2.3.float?
|
30
|
+
assert !BigDecimal('1').float?
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IntegerTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".integer?
|
6
|
+
assert "-1".integer?
|
7
|
+
assert !"1.0".integer?
|
8
|
+
|
9
|
+
assert !"a".integer?
|
10
|
+
assert !"1a".integer?
|
11
|
+
assert !"a1".integer?
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_symbol
|
15
|
+
assert :'0'.integer?
|
16
|
+
|
17
|
+
assert !:a.integer?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_object
|
21
|
+
assert !Object.new.integer?
|
22
|
+
assert !{}.integer?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_numeric
|
26
|
+
assert 1.integer?
|
27
|
+
assert !Rational(1,2).integer?
|
28
|
+
assert !Complex(1, 1).integer?
|
29
|
+
assert !2.3.integer?
|
30
|
+
assert !BigDecimal('1').integer?
|
31
|
+
end
|
32
|
+
end
|
data/test/numeric_test.rb
CHANGED
@@ -25,16 +25,11 @@ class NumericTest < Test::Unit::TestCase
|
|
25
25
|
assert !{}.numeric?
|
26
26
|
end
|
27
27
|
|
28
|
-
def test_basic_object
|
29
|
-
assert !BasicObject.new.numeric?
|
30
|
-
end
|
31
|
-
|
32
28
|
def test_numeric
|
33
|
-
require 'rational'
|
34
|
-
|
35
29
|
assert 1.numeric?
|
36
30
|
assert Rational(1,2).numeric?
|
37
31
|
assert Complex(1, 1).numeric?
|
38
32
|
assert 2.3.numeric?
|
33
|
+
assert BigDecimal('1').numeric?
|
39
34
|
end
|
40
35
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RationalTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".rational?
|
6
|
+
assert "-1".rational?
|
7
|
+
assert "1.0".rational?
|
8
|
+
|
9
|
+
assert !"a".rational?
|
10
|
+
assert !"1a".rational?
|
11
|
+
assert !"a1".rational?
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_symbol
|
15
|
+
assert :'0'.rational?
|
16
|
+
|
17
|
+
assert !:a.rational?
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_object
|
21
|
+
assert !Object.new.rational?
|
22
|
+
assert !{}.rational?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_numeric
|
26
|
+
assert !1.rational?
|
27
|
+
assert Rational(1,2).rational?
|
28
|
+
assert !Complex(1, 1).rational?
|
29
|
+
assert !2.3.rational?
|
30
|
+
assert !BigDecimal('1').rational?
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,49 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numeric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- David Butler
|
9
|
-
autorequire:
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-19 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
|
-
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
17
16
|
requirements:
|
18
17
|
- - ~>
|
19
18
|
- !ruby/object:Gem::Version
|
20
19
|
version: '1.3'
|
21
|
-
|
22
|
-
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
|
-
none: false
|
28
|
-
prerelease: false
|
29
|
-
type: :development
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
|
-
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
33
30
|
requirements:
|
34
31
|
- - '>='
|
35
32
|
- !ruby/object:Gem::Version
|
36
33
|
version: '0'
|
37
|
-
|
38
|
-
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
37
|
requirements:
|
40
38
|
- - '>='
|
41
39
|
- !ruby/object:Gem::Version
|
42
40
|
version: '0'
|
43
|
-
|
44
|
-
prerelease: false
|
45
|
-
type: :development
|
46
|
-
description: 'Adds #numeric? to Ruby'
|
41
|
+
description: Adds numeric checking to Ruby
|
47
42
|
email:
|
48
43
|
- dwbutler@ucla.edu
|
49
44
|
executables: []
|
@@ -59,12 +54,18 @@ files:
|
|
59
54
|
- VERSION
|
60
55
|
- lib/numeric.rb
|
61
56
|
- numeric.gemspec
|
57
|
+
- test/complex_test.rb
|
58
|
+
- test/decimal_test.rb
|
59
|
+
- test/float_test.rb
|
60
|
+
- test/integer_test.rb
|
62
61
|
- test/numeric_test.rb
|
62
|
+
- test/rational_test.rb
|
63
63
|
- test/test_helper.rb
|
64
|
-
homepage:
|
64
|
+
homepage: https://github.com/dwbutler/numeric
|
65
65
|
licenses:
|
66
66
|
- MIT
|
67
|
-
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
68
69
|
rdoc_options: []
|
69
70
|
require_paths:
|
70
71
|
- lib
|
@@ -72,26 +73,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
73
|
requirements:
|
73
74
|
- - '>='
|
74
75
|
- !ruby/object:Gem::Version
|
75
|
-
segments:
|
76
|
-
- 0
|
77
76
|
version: '0'
|
78
|
-
hash: 2
|
79
|
-
none: false
|
80
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
78
|
requirements:
|
82
79
|
- - '>='
|
83
80
|
- !ruby/object:Gem::Version
|
84
|
-
segments:
|
85
|
-
- 0
|
86
81
|
version: '0'
|
87
|
-
hash: 2
|
88
|
-
none: false
|
89
82
|
requirements: []
|
90
|
-
rubyforge_project:
|
91
|
-
rubygems_version:
|
92
|
-
signing_key:
|
93
|
-
specification_version:
|
94
|
-
summary:
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.0.3
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Adds numeric checking to Ruby
|
95
88
|
test_files:
|
89
|
+
- test/complex_test.rb
|
90
|
+
- test/decimal_test.rb
|
91
|
+
- test/float_test.rb
|
92
|
+
- test/integer_test.rb
|
96
93
|
- test/numeric_test.rb
|
94
|
+
- test/rational_test.rb
|
97
95
|
- test/test_helper.rb
|