numeric 0.1.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.
- data/.gitignore +17 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +10 -0
- data/VERSION +1 -0
- data/lib/numeric.rb +23 -0
- data/numeric.gemspec +23 -0
- data/test/numeric_test.rb +40 -0
- data/test/test_helper.rb +3 -0
- metadata +97 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 David Butler
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Numeric
|
2
|
+
|
3
|
+
Checking if a given value is "numeric" is a common problem. How can we do in this Ruby?
|
4
|
+
There are many problematic ways, such as converting to an integer or float or using a regex:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
"String".to_i
|
8
|
+
# => 0
|
9
|
+
|
10
|
+
nil.to_f
|
11
|
+
# => 0.0
|
12
|
+
|
13
|
+
:'0'.to_i
|
14
|
+
# NoMethodError: undefined method `to_i' for :"0":Symbol
|
15
|
+
|
16
|
+
"1e5" =~ /^\d*$/
|
17
|
+
=> nil
|
18
|
+
```
|
19
|
+
|
20
|
+
This gem defines a simple (and correct) `#numeric?` method on all Ruby objects.
|
21
|
+
The rules are simple:
|
22
|
+
|
23
|
+
* All descendants of `Numeric` are numeric.
|
24
|
+
* All strings or symbols that can be converted with `Complex()` are numeric.
|
25
|
+
* All other objects are not numeric.
|
26
|
+
|
27
|
+
## Compatibility
|
28
|
+
|
29
|
+
The gem is tested to work in MRI Ruby 1.9+ and JRuby (1.9 mode).
|
30
|
+
MRI 1.8.7, REE, and Rubinius all have bugs in their implementation of `Complex`
|
31
|
+
that prevent this gem from working completely, so they are not supported.
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
gem 'numeric'
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install numeric
|
46
|
+
|
47
|
+
## Usage
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'numeric'
|
51
|
+
|
52
|
+
3.numeric?
|
53
|
+
# => true
|
54
|
+
|
55
|
+
'1'.numeric?
|
56
|
+
# => true
|
57
|
+
|
58
|
+
'3e10'.numeric?
|
59
|
+
# => true
|
60
|
+
|
61
|
+
:'0'.numeric?
|
62
|
+
# => true
|
63
|
+
|
64
|
+
'abc123'.numeric?
|
65
|
+
# => false
|
66
|
+
|
67
|
+
{}.numeric?
|
68
|
+
# => false
|
69
|
+
```
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
1. Fork it
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
77
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/numeric.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class String
|
2
|
+
def numeric?
|
3
|
+
!!Complex(self) rescue false
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Symbol
|
8
|
+
def numeric?
|
9
|
+
to_s.numeric?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Numeric
|
14
|
+
def numeric?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class BasicObject
|
20
|
+
def numeric?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
data/numeric.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
version = File.open(File.expand_path('../VERSION', __FILE__)).read
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "numeric"
|
8
|
+
spec.version = version
|
9
|
+
spec.authors = ["David Butler"]
|
10
|
+
spec.email = ["dwbutler@ucla.edu"]
|
11
|
+
spec.description = %q{Adds #numeric? to Ruby}
|
12
|
+
spec.summary = %q{Adds #numeric? to Ruby}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NumericTest < Test::Unit::TestCase
|
4
|
+
def test_string
|
5
|
+
assert "0".numeric?
|
6
|
+
assert "-1".numeric?
|
7
|
+
assert "1.0".numeric?
|
8
|
+
assert "1e1".numeric?
|
9
|
+
assert "1/2".numeric?
|
10
|
+
assert "0.3-0.5i".numeric?
|
11
|
+
|
12
|
+
assert !"a".numeric?
|
13
|
+
assert !"1a".numeric?
|
14
|
+
assert !"a1".numeric?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_symbol
|
18
|
+
assert :'0'.numeric?
|
19
|
+
|
20
|
+
assert !:a.numeric?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_object
|
24
|
+
assert !Object.new.numeric?
|
25
|
+
assert !{}.numeric?
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_basic_object
|
29
|
+
assert !BasicObject.new.numeric?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_numeric
|
33
|
+
require 'rational'
|
34
|
+
|
35
|
+
assert 1.numeric?
|
36
|
+
assert Rational(1,2).numeric?
|
37
|
+
assert Complex(1, 1).numeric?
|
38
|
+
assert 2.3.numeric?
|
39
|
+
end
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: numeric
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- David Butler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
none: false
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
none: false
|
28
|
+
prerelease: false
|
29
|
+
type: :development
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
none: false
|
38
|
+
requirement: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
none: false
|
44
|
+
prerelease: false
|
45
|
+
type: :development
|
46
|
+
description: 'Adds #numeric? to Ruby'
|
47
|
+
email:
|
48
|
+
- dwbutler@ucla.edu
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- VERSION
|
60
|
+
- lib/numeric.rb
|
61
|
+
- numeric.gemspec
|
62
|
+
- test/numeric_test.rb
|
63
|
+
- test/test_helper.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: '0'
|
78
|
+
hash: 2
|
79
|
+
none: false
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: '0'
|
87
|
+
hash: 2
|
88
|
+
none: false
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: 'Adds #numeric? to Ruby'
|
95
|
+
test_files:
|
96
|
+
- test/numeric_test.rb
|
97
|
+
- test/test_helper.rb
|