nutella 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/lib/nutella/core_ext/integer.rb +32 -0
- data/lib/nutella/version.rb +1 -1
- data/spec/nutella/core_ext/integer_spec.rb +74 -0
- metadata +6 -3
data/README.md
CHANGED
@@ -3,7 +3,8 @@
|
|
3
3
|
This library is my personal collection of Ruby utilities, mainly core
|
4
4
|
extensions.
|
5
5
|
|
6
|
-
* [RubyGems](https://rubygems.org/gems/nutella)
|
6
|
+
* [RubyGems Release Page](https://rubygems.org/gems/nutella)
|
7
|
+
* [Documentation on RubyDoc](http://rubydoc.info/github/gbchaosmaster/nutella/master/frames)
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Integer
|
2
|
+
# Check whether the integer is evenly divisible by all of the arguments.
|
3
|
+
#
|
4
|
+
# @example Check if some numbers are evenly divisible by 5
|
5
|
+
# 4.multiple_of? 5 # => false
|
6
|
+
# 30.multiple_of? 5 # => true
|
7
|
+
#
|
8
|
+
# @example Check if some numbers are evenly divisible by both 4 and 7
|
9
|
+
# 10.multiple_of? 4, 7 # => false
|
10
|
+
# 56.multiple_of? 4, 7 # => true
|
11
|
+
#
|
12
|
+
# @param [*Integer] nums the integer(s) to check against
|
13
|
+
# @return [Boolean] whether or not the integer is evenly divisible by all
|
14
|
+
# arguments
|
15
|
+
def multiple_of?(*nums)
|
16
|
+
nums.all? { |n| (!n.zero? && self % n == 0) || zero? }
|
17
|
+
end
|
18
|
+
alias_method :divisible_by?, :multiple_of?
|
19
|
+
|
20
|
+
# Check whether the integer is evenly divisible by any of the arguments.
|
21
|
+
#
|
22
|
+
# @example {Project Euler #1}[http://projecteuler.net/problem=1] solution using Nutella
|
23
|
+
# (1...1000).select { |n| n.multiple_of_any?(3, 5) }.sum
|
24
|
+
#
|
25
|
+
# @param [*Integer] nums the integer(s) to check against
|
26
|
+
# @return [Boolean] whether or not the integer is evenly divisible by any
|
27
|
+
# arguments
|
28
|
+
def multiple_of_any?(*nums)
|
29
|
+
nums.any? { |n| multiple_of? n }
|
30
|
+
end
|
31
|
+
alias_method :divisible_by_any?, :multiple_of_any?
|
32
|
+
end
|
data/lib/nutella/version.rb
CHANGED
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "nutella/core_ext/integer"
|
3
|
+
|
4
|
+
describe Integer do
|
5
|
+
describe "aliases" do
|
6
|
+
test_alias Integer, :divisible_by?, :multiple_of?
|
7
|
+
test_alias Integer, :divisible_by_any?, :multiple_of_any?
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#multiple_of?" do
|
11
|
+
it "should return true if the number is evenly divisible" do
|
12
|
+
5.multiple_of?(5).should be_true
|
13
|
+
15.multiple_of?(5).should be_true
|
14
|
+
10.multiple_of?(2).should be_true
|
15
|
+
6000.multiple_of?(6).should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return false if the number is not evenly divisible" do
|
19
|
+
20.multiple_of?(7).should be_false
|
20
|
+
4.multiple_of?(3).should be_false
|
21
|
+
5.multiple_of?(15).should be_false
|
22
|
+
100.multiple_of?(3).should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when passing in zero" do
|
26
|
+
it "should return false if one tries to divide by zero" do
|
27
|
+
20.multiple_of?(0).should be_false
|
28
|
+
30.multiple_of?(0).should be_false
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should allow zero to go into zero" do
|
32
|
+
0.multiple_of?(0).should be_true
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "with multiple arguments" do
|
37
|
+
it "should return true if evenly divisible by all arguments" do
|
38
|
+
15.multiple_of?(3, 15).should be_true
|
39
|
+
100.multiple_of?(2, 5, 25).should be_true
|
40
|
+
0.multiple_of?(0, 1, 2).should be_true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return false if evenly divisible by only some arguments" do
|
44
|
+
15.multiple_of?(2, 3).should be_false
|
45
|
+
12.multiple_of?(3, 4, 6, 8).should be_false
|
46
|
+
10.multiple_of?(0, 5).should be_false
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return false if evenly divisible by none of the arguments" do
|
50
|
+
6.multiple_of?(4, 5).should be_false
|
51
|
+
17.multiple_of?(2, 4).should be_false
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#multiple_of_any?" do
|
57
|
+
it "should return true if evenly divisible by all arguments" do
|
58
|
+
15.multiple_of_any?(3, 15).should be_true
|
59
|
+
100.multiple_of_any?(2, 5, 25).should be_true
|
60
|
+
0.multiple_of_any?(0, 1, 2).should be_true
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return true if evenly divisible by only some arguments" do
|
64
|
+
15.multiple_of_any?(2, 3).should be_true
|
65
|
+
12.multiple_of_any?(3, 4, 6, 8).should be_true
|
66
|
+
10.multiple_of_any?(0, 5).should be_true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return false if evenly divisible by none of the arguments" do
|
70
|
+
6.multiple_of_any?(4, 5).should be_false
|
71
|
+
17.multiple_of_any?(2, 4).should be_false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-09-
|
12
|
+
date: 2012-09-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -100,11 +100,13 @@ files:
|
|
100
100
|
- lib/nutella.rb
|
101
101
|
- lib/nutella/version.rb
|
102
102
|
- lib/nutella/core_ext/string.rb
|
103
|
+
- lib/nutella/core_ext/integer.rb
|
103
104
|
- lib/nutella/core_ext/hash.rb
|
104
105
|
- lib/nutella/core_ext/enumerable.rb
|
105
106
|
- lib/nutella/core_ext.rb
|
106
107
|
- spec/nutella/core_ext/hash_spec.rb
|
107
108
|
- spec/nutella/core_ext/enumerable_spec.rb
|
109
|
+
- spec/nutella/core_ext/integer_spec.rb
|
108
110
|
- spec/nutella/core_ext/string_spec.rb
|
109
111
|
- spec/spec_helper.rb
|
110
112
|
- .rspec
|
@@ -133,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
135
|
version: '0'
|
134
136
|
segments:
|
135
137
|
- 0
|
136
|
-
hash:
|
138
|
+
hash: 1233150412608386870
|
137
139
|
requirements: []
|
138
140
|
rubyforge_project:
|
139
141
|
rubygems_version: 1.8.24
|
@@ -143,6 +145,7 @@ summary: Spread some Nutella on Ruby to sweeten up its functionality.
|
|
143
145
|
test_files:
|
144
146
|
- spec/nutella/core_ext/hash_spec.rb
|
145
147
|
- spec/nutella/core_ext/enumerable_spec.rb
|
148
|
+
- spec/nutella/core_ext/integer_spec.rb
|
146
149
|
- spec/nutella/core_ext/string_spec.rb
|
147
150
|
- spec/spec_helper.rb
|
148
151
|
has_rdoc:
|