nobiru 0.0.1 → 1.0.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 +4 -4
- data/README.md +72 -6
- data/lib/nobiru/extensions/enumerable_extension.rb +36 -28
- data/lib/nobiru/extensions/numeric_extension.rb +20 -0
- data/lib/nobiru/version.rb +1 -1
- data/spec/lib/enumerable_extension_spec.rb +56 -0
- data/spec/lib/numeric_extension_spec.rb +30 -0
- data/spec/spec_helper.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70c345d373e4f0f14a2e40aee818e2d1e28a6f4f
|
4
|
+
data.tar.gz: 0ce183109d6578d588518b3124befaad51c87d74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f1f89c481901ccdcbc62fc347e15644208254d908cfdc1f91061745eb531224b78140108243a0b03b2163b3a5f201412ba855696f50c0c3ef149bf499477a7f
|
7
|
+
data.tar.gz: 1d0e43867268bad43ba45303bc16ae02b48c13a69779eb17761604e11da391b56feda1d96246640ee042545c3f293d6161e289df293deffd29b8ff2838dadf20
|
data/README.md
CHANGED
@@ -3,9 +3,8 @@
|
|
3
3
|
[](http://badge.fury.io/rb/nobiru)
|
4
4
|
[](https://travis-ci.org/drexed/nobiru)
|
5
5
|
[](https://coveralls.io/r/drexed/nobiru)
|
6
|
-
[](https://codeclimate.com/github/drexed/nobiru)
|
7
6
|
|
8
|
-
Nobiru is a collection of commonly used object helpers in a ruby based project. It currently includes extensions for: arrays, enumerables, hashes, numerics, objects, strings, and time.
|
7
|
+
Nobiru (japanese: extend) is a collection of commonly used object helpers in a ruby based project. It currently includes extensions for: arrays, enumerables, hashes, numerics, objects, strings, and time.
|
9
8
|
|
10
9
|
`Rails Safe` = methods extracted from rails but that do not override that rails method.
|
11
10
|
|
@@ -57,6 +56,23 @@ Use the `remove_last` method removes the last element from an array. Like Array.
|
|
57
56
|
|
58
57
|
### EnumerableExtensions
|
59
58
|
|
59
|
+
####Difference:####
|
60
|
+
Use the `difference` method to return the difference of a collection of numbers.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
[1,2,3].difference #=> -4
|
64
|
+
[].difference #=> 0
|
65
|
+
[].difference(nil) #=> nil
|
66
|
+
```
|
67
|
+
|
68
|
+
Use the `divisible` method to return the division of a collection of numbers.
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
[16,4,2].divisible #=> 2
|
72
|
+
[].divisible #=> 0
|
73
|
+
[].divisible(nil) #=> nil
|
74
|
+
```
|
75
|
+
|
60
76
|
####Drop Last:####
|
61
77
|
Use the `drop_last` method to drops the last number of elements of a collection.
|
62
78
|
|
@@ -82,6 +98,14 @@ Use the `exactly?` method to return if there are exactly the number of an elemen
|
|
82
98
|
[].exactly?(1) #=> false
|
83
99
|
```
|
84
100
|
|
101
|
+
Use the `exponential` method to return the exponential of a collection of numbers.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
[2,3,4].exponential #=> 4096
|
105
|
+
[].exponential #=> 0
|
106
|
+
[].exponential(nil) #=> nil
|
107
|
+
```
|
108
|
+
|
85
109
|
####Frequencies:####
|
86
110
|
Use the `frequencies` method to return a hash of the number of times a value in an array appears.
|
87
111
|
|
@@ -130,12 +154,20 @@ Use the `median` method to return the middle value of a collection of numbers.
|
|
130
154
|
Use the `mode` method to return the most frequent value of a collection of numbers.
|
131
155
|
|
132
156
|
```ruby
|
133
|
-
[1,1,2,
|
157
|
+
[1,1,2,6].mode #=> 1
|
134
158
|
[1,2,3].mode #=> nil
|
135
159
|
[].mode #=> 0
|
136
160
|
[].mode(nil) #=> nil
|
137
161
|
```
|
138
162
|
|
163
|
+
Use the `multiple` method to return the multiplication of a collection of numbers.
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
[1,2,3].multiple #=> 6
|
167
|
+
[].multiple #=> 0
|
168
|
+
[].multiple(nil) #=> nil
|
169
|
+
```
|
170
|
+
|
139
171
|
####Range:####
|
140
172
|
Use the `range` method to return the difference between the smallest and largest value of a collection of numbers.
|
141
173
|
|
@@ -163,11 +195,10 @@ Use the `standard_deviation` method to return the standard deviation of elements
|
|
163
195
|
```
|
164
196
|
|
165
197
|
####Sum:####
|
166
|
-
Use the `sum` method to
|
198
|
+
Use the `sum` method to return the sum of a collection of numbers.
|
167
199
|
|
168
200
|
```ruby
|
169
|
-
[1,2,3].sum #=>
|
170
|
-
[1,2,3,4].sum #=> 2.5
|
201
|
+
[1,2,3].sum #=> 6
|
171
202
|
[].sum #=> 0
|
172
203
|
[].sum(nil) #=> nil
|
173
204
|
```
|
@@ -291,6 +322,27 @@ Use the `try` method on a object to try that method with out raising an error. `
|
|
291
322
|
|
292
323
|
### NumericExtensions
|
293
324
|
|
325
|
+
####Add:####
|
326
|
+
Use the `add` method to add two numbers.
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
4.add(2) #=> 6
|
330
|
+
```
|
331
|
+
|
332
|
+
####Divide:####
|
333
|
+
Use the `divide` method to divide two numbers.
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
4.divide(2) #=> 2
|
337
|
+
```
|
338
|
+
|
339
|
+
####Multiply:####
|
340
|
+
Use the `multiply` method to multiply two numbers.
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
4.multiply(2) #=> 8
|
344
|
+
```
|
345
|
+
|
294
346
|
####Multiple Of:####
|
295
347
|
Use the `multiple_of?` method to check if a number is the multiple of another. `Rails Safe`
|
296
348
|
|
@@ -315,6 +367,20 @@ Use the `positive?` method to check if a number is positive.
|
|
315
367
|
-1.positive? #=> false
|
316
368
|
```
|
317
369
|
|
370
|
+
####Power:####
|
371
|
+
Use the `power` method to return the power of two numbers.
|
372
|
+
|
373
|
+
```ruby
|
374
|
+
4.power(2) #=> 16
|
375
|
+
```
|
376
|
+
|
377
|
+
####Subtract:####
|
378
|
+
Use the `subtract` method to subtract two numbers.
|
379
|
+
|
380
|
+
```ruby
|
381
|
+
4.subtract(2) #=> 2
|
382
|
+
```
|
383
|
+
|
318
384
|
####To Byte:####
|
319
385
|
Use the `to_byte` method to convert a byte size from one unit to another unit.
|
320
386
|
|
@@ -6,15 +6,23 @@ module Enumerable
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
+
def difference(identity=0)
|
10
|
+
inject(:-) || identity
|
11
|
+
end
|
12
|
+
|
13
|
+
def divisible(identity=0)
|
14
|
+
inject(:/) || identity
|
15
|
+
end
|
16
|
+
|
9
17
|
def drop_last(n)
|
10
18
|
array = to_a
|
11
19
|
|
12
|
-
return
|
20
|
+
return(array) if n > array.size
|
13
21
|
array[0...(array.size - n)]
|
14
22
|
end
|
15
23
|
|
16
24
|
def drop_last_while
|
17
|
-
return
|
25
|
+
return(to_enum(:drop_last_while)) unless block_given?
|
18
26
|
|
19
27
|
result = []
|
20
28
|
dropping = true
|
@@ -28,18 +36,18 @@ module Enumerable
|
|
28
36
|
found_count = 0
|
29
37
|
|
30
38
|
if block_given?
|
31
|
-
each
|
32
|
-
found_count += 1 if yield(*o)
|
33
|
-
end
|
39
|
+
each { |*o| found_count += 1 if yield(*o) }
|
34
40
|
else
|
35
|
-
each
|
36
|
-
found_count += 1 if o
|
37
|
-
end
|
41
|
+
each { |o| found_count += 1 if o }
|
38
42
|
end
|
39
43
|
|
40
44
|
(found_count > n) ? false : n == found_count
|
41
45
|
end
|
42
46
|
|
47
|
+
def exponential(identity=0)
|
48
|
+
inject(:**) || identity
|
49
|
+
end
|
50
|
+
|
43
51
|
def frequencies
|
44
52
|
each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
|
45
53
|
end
|
@@ -62,10 +70,10 @@ module Enumerable
|
|
62
70
|
collection_sorted = sort
|
63
71
|
|
64
72
|
if collection_size > 0
|
65
|
-
if collection_size % 2
|
66
|
-
(collection_sorted[(collection_size / 2) -1] + collection_sorted[collection_size / 2]) / 2.0
|
73
|
+
if (collection_size % 2).zero?
|
74
|
+
(collection_sorted[(collection_size / 2.0) -1.0] + collection_sorted[collection_size / 2.0]) / 2.0
|
67
75
|
else
|
68
|
-
collection_sorted[collection_size / 2]
|
76
|
+
collection_sorted[collection_size / 2.0]
|
69
77
|
end
|
70
78
|
else
|
71
79
|
identity
|
@@ -75,20 +83,24 @@ module Enumerable
|
|
75
83
|
def mode(identity=0)
|
76
84
|
if size > 0
|
77
85
|
frequency_distribution = inject(Hash.new(0)) { |h, v| h[v] += 1; h }
|
78
|
-
|
86
|
+
frequency_top_two = frequency_distribution.sort { |a, b| b[1] <=> a[1] }.take(2)
|
79
87
|
|
80
|
-
if
|
81
|
-
|
82
|
-
elsif
|
88
|
+
if frequency_top_two.length == 1
|
89
|
+
frequency_top_two.first.first
|
90
|
+
elsif frequency_top_two.first.last == frequency_top_two.last.last
|
83
91
|
nil
|
84
92
|
else
|
85
|
-
|
93
|
+
frequency_top_two.first.first
|
86
94
|
end
|
87
95
|
else
|
88
96
|
identity
|
89
97
|
end
|
90
98
|
end
|
91
99
|
|
100
|
+
def multiple(identity=0)
|
101
|
+
inject(:*) || identity
|
102
|
+
end
|
103
|
+
|
92
104
|
def range(identity=0)
|
93
105
|
collection_sorted = sort
|
94
106
|
size > 0 ? collection_sorted.last - collection_sorted.first : identity
|
@@ -98,32 +110,28 @@ module Enumerable
|
|
98
110
|
found_count = 0
|
99
111
|
|
100
112
|
if block_given?
|
101
|
-
each
|
102
|
-
found_count += 1 if yield(*o)
|
103
|
-
end
|
113
|
+
each { |*o| found_count += 1 if yield(*o) }
|
104
114
|
else
|
105
|
-
each
|
106
|
-
found_count += 1 if o
|
107
|
-
end
|
115
|
+
each { |o| found_count += 1 if o }
|
108
116
|
end
|
109
117
|
|
110
118
|
(found_count > 1) ? true : false
|
111
119
|
end
|
112
120
|
|
113
121
|
def standard_deviation
|
114
|
-
return
|
122
|
+
return(0) if length < 2
|
115
123
|
Math.sqrt(variance)
|
116
124
|
end
|
117
125
|
|
118
126
|
def take_last(n)
|
119
127
|
array = to_a
|
120
128
|
|
121
|
-
return
|
129
|
+
return(array) if n > array.size
|
122
130
|
array[(array.size - n)..-1]
|
123
131
|
end
|
124
132
|
|
125
133
|
def take_last_while
|
126
|
-
return
|
134
|
+
return(to_enum(:take_last_while)) unless block_given?
|
127
135
|
|
128
136
|
result = []
|
129
137
|
reverse_each { |e| yield(e) ? result.unshift(e) : break }
|
@@ -133,9 +141,9 @@ module Enumerable
|
|
133
141
|
def variance
|
134
142
|
collection_length = length
|
135
143
|
|
136
|
-
return
|
137
|
-
sum = inject(0) { |accumulator, value| accumulator + (value - mean) ** 2 }
|
138
|
-
sum / (collection_length.to_f - 1)
|
144
|
+
return(0) if collection_length <= 1
|
145
|
+
sum = inject(0.0) { |accumulator, value| accumulator + (value - mean) ** 2.0 }
|
146
|
+
sum / (collection_length.to_f - 1.0)
|
139
147
|
end
|
140
148
|
|
141
149
|
end
|
@@ -1,5 +1,17 @@
|
|
1
1
|
class Numeric
|
2
2
|
|
3
|
+
def add(n)
|
4
|
+
self + n
|
5
|
+
end
|
6
|
+
|
7
|
+
def divide(n)
|
8
|
+
self / n
|
9
|
+
end
|
10
|
+
|
11
|
+
def multiply(n)
|
12
|
+
self * n
|
13
|
+
end
|
14
|
+
|
3
15
|
unless method_defined?(:multiple_of?)
|
4
16
|
def multiple_of?(number)
|
5
17
|
number != 0 ? modulo(number).zero? : zero?
|
@@ -14,6 +26,14 @@ class Numeric
|
|
14
26
|
self > 0
|
15
27
|
end
|
16
28
|
|
29
|
+
def power(n)
|
30
|
+
self ** n
|
31
|
+
end
|
32
|
+
|
33
|
+
def subtract(n)
|
34
|
+
self - n
|
35
|
+
end
|
36
|
+
|
17
37
|
def to_byte(from=:b, to=:kb)
|
18
38
|
scalers = { b: 1, kb: 1024 ** 1, mb: 1024 ** 2, gb: 1024 ** 3, tb: 1024 ** 4, pb: 1024 ** 5, eb: 1024 ** 6 }
|
19
39
|
|
data/lib/nobiru/version.rb
CHANGED
@@ -2,6 +2,34 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Enumerable do
|
4
4
|
|
5
|
+
describe "#difference" do
|
6
|
+
it "to be 0" do
|
7
|
+
expect([].difference).to eq(0)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "to be nil" do
|
11
|
+
expect([].difference(nil)).to eq(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "to be -4" do
|
15
|
+
expect([1, 2, 3].difference).to eq(-4)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#divisible" do
|
20
|
+
it "to be 0" do
|
21
|
+
expect([].divisible).to eq(0)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "to be nil" do
|
25
|
+
expect([].divisible(nil)).to eq(nil)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "to be 2" do
|
29
|
+
expect([16, 4, 2].divisible).to eq(2)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
5
33
|
describe "#drop_last" do
|
6
34
|
it "to be [2, 3]" do
|
7
35
|
expect([1, 2, 3].drop_last(1)).to eq([1, 2])
|
@@ -38,6 +66,20 @@ describe Enumerable do
|
|
38
66
|
end
|
39
67
|
end
|
40
68
|
|
69
|
+
describe "#exponential" do
|
70
|
+
it "to be 0" do
|
71
|
+
expect([].exponential).to eq(0)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "to be nil" do
|
75
|
+
expect([].exponential(nil)).to eq(nil)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "to be 4096" do
|
79
|
+
expect([2, 3, 4].exponential).to eq(4096)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
41
83
|
describe "#frequencies" do
|
42
84
|
it "to be {}" do
|
43
85
|
expect([].frequencies).to eq({})
|
@@ -131,6 +173,20 @@ describe Enumerable do
|
|
131
173
|
end
|
132
174
|
end
|
133
175
|
|
176
|
+
describe "#multiple" do
|
177
|
+
it "to be 0" do
|
178
|
+
expect([].multiple).to eq(0)
|
179
|
+
end
|
180
|
+
|
181
|
+
it "to be nil" do
|
182
|
+
expect([].multiple(nil)).to eq(nil)
|
183
|
+
end
|
184
|
+
|
185
|
+
it "to be 6" do
|
186
|
+
expect([1, 2, 3].multiple).to eq(6)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
134
190
|
describe '#range' do
|
135
191
|
it 'to be 0' do
|
136
192
|
expect([].range).to eq(0)
|
@@ -2,6 +2,24 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Numeric do
|
4
4
|
|
5
|
+
describe "#add" do
|
6
|
+
it "to be 3" do
|
7
|
+
expect(4.add(2)).to eq(6)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#divide" do
|
12
|
+
it "to be 3" do
|
13
|
+
expect(4.divide(2)).to eq(2)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#multiply" do
|
18
|
+
it "to be 3" do
|
19
|
+
expect(4.multiply(2)).to eq(8)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
5
23
|
describe "#multiple_of?" do
|
6
24
|
it "to be true" do
|
7
25
|
expect(9.multiple_of?(3)).to eq(true)
|
@@ -32,6 +50,18 @@ describe Numeric do
|
|
32
50
|
end
|
33
51
|
end
|
34
52
|
|
53
|
+
describe "#power" do
|
54
|
+
it "to be 6" do
|
55
|
+
expect(4.power(2)).to eq(16)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#subtract" do
|
60
|
+
it "to be 2" do
|
61
|
+
expect(4.subtract(2)).to eq(2)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
35
65
|
describe "#to_byte" do
|
36
66
|
it "to be 1" do
|
37
67
|
expect(1024.to_byte).to eq(1)
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nobiru
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.4.
|
122
|
+
rubygems_version: 2.4.6
|
123
123
|
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: Commonly used ruby object helpers.
|