array_extend 1.0.0 → 1.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/README.md +96 -3
- data/array_extend.gemspec +1 -1
- data/lib/array_extend.rb +26 -2
- data/lib/array_extend/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
|
@@ -18,30 +18,39 @@ Or install it yourself as:
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
### after
|
|
22
22
|
|
|
23
23
|
Return the value after the one pass in argument
|
|
24
24
|
|
|
25
25
|
Demo
|
|
26
26
|
|
|
27
|
+
```ruby
|
|
27
28
|
["hello", "world"].after("hello") #=> "world"
|
|
28
29
|
["hello", "world"].after("world") #=> nil
|
|
29
30
|
["hello", "world"].after("none") #=> nil
|
|
31
|
+
```
|
|
30
32
|
|
|
31
33
|
Code
|
|
32
34
|
|
|
35
|
+
```ruby
|
|
33
36
|
def after val
|
|
34
37
|
self[index(val) + 1] if self.include? val
|
|
35
38
|
end
|
|
39
|
+
```
|
|
36
40
|
|
|
37
41
|
|
|
38
|
-
|
|
39
|
-
* stealth_delete *vals
|
|
42
|
+
### stealth_delete
|
|
40
43
|
|
|
41
44
|
Delete value(s) passed by, but return the array instead of the deleted value
|
|
42
45
|
|
|
46
|
+
```ruby
|
|
47
|
+
stealth_delete! *vals
|
|
48
|
+
stealth_delete *vals
|
|
49
|
+
```
|
|
50
|
+
|
|
43
51
|
Demo
|
|
44
52
|
|
|
53
|
+
```ruby
|
|
45
54
|
["ruby", "the", "programmer", "best", "friend"].stealth_delete! "ruby"
|
|
46
55
|
#=> ["the", "programmer", "best", "friend"]
|
|
47
56
|
|
|
@@ -50,15 +59,99 @@ Demo
|
|
|
50
59
|
|
|
51
60
|
["ruby", "the", "programmer", "best", "friend"].stealth_delete! "hello"
|
|
52
61
|
#=> ["ruby", "the", "programmer", "best", "friend"]
|
|
62
|
+
```
|
|
53
63
|
|
|
54
64
|
Code
|
|
55
65
|
|
|
66
|
+
```ruby
|
|
56
67
|
def stealth_delete! *vals
|
|
57
68
|
vals.each do |val|
|
|
58
69
|
delete val
|
|
59
70
|
end
|
|
60
71
|
return self
|
|
61
72
|
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### tautology?
|
|
78
|
+
|
|
79
|
+
Return true if all elements in array are presents. Usefull to check if all passed_by args are -really- presents for example.
|
|
80
|
+
|
|
81
|
+
Demo
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
[true, true, true].tautology? #=> true
|
|
85
|
+
[true, false, true].tautology? #=> false
|
|
86
|
+
[1, 2, "hello", "world", {:one => 1}].tautology? #=> true
|
|
87
|
+
[1, 2, nil, "hello"].tautology? #=> false
|
|
88
|
+
[1, 2, [], {}].tautology? #=> false
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Code
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
def tautology?
|
|
95
|
+
self.inject(true){ |res, elt| res && elt.present? }
|
|
96
|
+
end
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
### compact_by
|
|
101
|
+
|
|
102
|
+
Allow to compact, only by one side (right or left). Usefull to delete nil values to the end of an array
|
|
103
|
+
BTW : I use here the rails "blank?" method, instead of the ruby "nil?" that is used in array#compact!
|
|
104
|
+
|
|
105
|
+
```ruby
|
|
106
|
+
compact_by! side
|
|
107
|
+
compact_by side
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Arguments : 'side' must be in %w{right left}
|
|
111
|
+
|
|
112
|
+
Demo
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
|
|
116
|
+
[nil, nil, "hello", "world", nil].compact_by :right
|
|
117
|
+
#=> [nil, nil, "hello", "world"]
|
|
118
|
+
|
|
119
|
+
[nil, nil, "hello", "world", nil].compact_by :left
|
|
120
|
+
#=> ["hello", "world", nil]
|
|
121
|
+
|
|
122
|
+
[nil, nil, "hello", nil, "world"].compact_by :left
|
|
123
|
+
#=> ["hello", nil, "world"]
|
|
124
|
+
|
|
125
|
+
[nil, [], "hello", nil, "world"].compact_by :left
|
|
126
|
+
#=> #=> ["hello", "world"]
|
|
127
|
+
|
|
128
|
+
["hello", "world"].compact_by :left
|
|
129
|
+
#=> ["hello", "world"]
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Code
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
def compact_by! side
|
|
136
|
+
case side
|
|
137
|
+
when :right, "right"
|
|
138
|
+
while self.last.blank?
|
|
139
|
+
self.pop
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
when :left, "left"
|
|
143
|
+
while self.first.blank?
|
|
144
|
+
self.shift
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
else
|
|
148
|
+
raise ArgumentError, "'side' argument must be in %w{right left}"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
self
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
62
155
|
|
|
63
156
|
## Contributing
|
|
64
157
|
|
data/array_extend.gemspec
CHANGED
|
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
|
|
|
5
5
|
gem.authors = ["Thomas Petrachi"]
|
|
6
6
|
gem.email = ["thomas.petrachi@vodeclic.com"]
|
|
7
7
|
gem.description = %q{Extend ruby Array. No override.}
|
|
8
|
-
gem.summary = %q{Adding methods
|
|
8
|
+
gem.summary = %q{Adding methods %w{after stealth_delete tautology? compact_by}}
|
|
9
9
|
gem.homepage = "https://github.com/petrachi/array_extend"
|
|
10
10
|
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
data/lib/array_extend.rb
CHANGED
|
@@ -11,11 +11,35 @@ class Array
|
|
|
11
11
|
vals.each do |val|
|
|
12
12
|
delete val
|
|
13
13
|
end
|
|
14
|
-
return self
|
|
15
14
|
end
|
|
16
15
|
|
|
16
|
+
# maps an '&&' operation through all elements of array
|
|
17
|
+
def tautology?
|
|
18
|
+
self.inject(true){ |res, elt| res && elt.present? }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# allow to compact by left side, or right side. this only delete the "extreme positioned" nils values
|
|
22
|
+
def compact_by! side
|
|
23
|
+
case side
|
|
24
|
+
when :right, "right"
|
|
25
|
+
while self.last.blank?
|
|
26
|
+
self.pop
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
when :left, "left"
|
|
30
|
+
while self.first.blank?
|
|
31
|
+
self.shift
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
else
|
|
35
|
+
raise ArgumentError, "'side' argument must be in %w{right left}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
self
|
|
39
|
+
end
|
|
40
|
+
|
|
17
41
|
# duplicate method without self modification
|
|
18
|
-
[:stealth_delete].each do |method_name|
|
|
42
|
+
[:stealth_delete, :compact_by].each do |method_name|
|
|
19
43
|
define_method method_name do |*args|
|
|
20
44
|
array = self.dup
|
|
21
45
|
eval "array.#{ method_name }! *args"
|
data/lib/array_extend/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: array_extend
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.0
|
|
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
|
description: Extend ruby Array. No override.
|
|
15
15
|
email:
|
|
@@ -49,5 +49,5 @@ rubyforge_project:
|
|
|
49
49
|
rubygems_version: 1.8.24
|
|
50
50
|
signing_key:
|
|
51
51
|
specification_version: 3
|
|
52
|
-
summary: Adding methods
|
|
52
|
+
summary: Adding methods %w{after stealth_delete tautology? compact_by}
|
|
53
53
|
test_files: []
|