pied_piper 0.1.4 → 0.1.5
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/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +51 -9
- data/lib/pied_piper.rb +10 -4
- data/lib/pied_piper/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 28891debbf93a2a534057564d68b0fa12eb56a320634e495d084efe62df74651
|
|
4
|
+
data.tar.gz: 5f77d34e71bf2e0ec423d967dd7eb92edf2012f28cad208cb802e3848040d009
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9b686c4a5412660cae523bd19d9ca76aae68764680801d1ec8bf742928236a07c14a3f7df8e49a454f3ced3b6178e60fa0386125b3565ae5d24c9d2e6a61303a
|
|
7
|
+
data.tar.gz: a4a1c639b8c1abcb819ca4990b6161189190c84e3e29bc9c26cbcf951c0c5b86c497a9df35dcce0cf96859fff085c2cc657eff8b4f3f0abdf3d0d9ed208566fc
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -113,40 +113,62 @@ Thus the `PiedPiper` class was born.
|
|
|
113
113
|
|
|
114
114
|
### Ruby and its Kernel module
|
|
115
115
|
|
|
116
|
-
If you want to add pipe functionality everywhere, we already talked about how to implement it above by requiring `pied_piper/kernel
|
|
116
|
+
If you want to add pipe functionality everywhere, we already talked about how to implement it above by requiring `pied_piper/kernel`.
|
|
117
117
|
|
|
118
|
-
This will provide pipe functionality on every object which has `Object` in one of its superclasses ( thus practically every object in Ruby besides `BasicObject` ) with the least amount of monkey-patching/side-effects, since we only add one Kernel method named `piper` and don't alter existing behaviour.
|
|
118
|
+
This will provide pipe functionality on every object which has `Object` in one of its superclasses ( thus practically every object in Ruby besides `BasicObject` which is the highest class in Ruby's inheritance hierarchy ) with the least amount of monkey-patching/side-effects, since we only add one `Kernel` method named `piper` and don't alter existing behaviour.
|
|
119
119
|
|
|
120
120
|
In case you didn't know:
|
|
121
121
|
|
|
122
122
|
`Object` includes `Kernel` as a module.
|
|
123
123
|
|
|
124
|
+
Included modules can be seen in Ruby like this:
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
Object.included_modules
|
|
128
|
+
# => [Kernel]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
If you want to see the whole inheritance chain of a class (with superclasses and included/prepended modules) you can do this:
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
Object.ancestors
|
|
135
|
+
# => [Object, Kernel, BasicObject]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Since Object `includes Kernel`, `Kernel` follows after `Object`.
|
|
139
|
+
|
|
140
|
+
If `Object` would `prepend Kernel`, `Kernel` would be before `Object`.
|
|
141
|
+
|
|
124
142
|
Methods who are intended to be globally available, like `puts` and `gets`, and who aren't intended to be available with an explicit receiver like `"foo".puts` are defined as private instance methods on `Kernel`.
|
|
125
143
|
|
|
126
|
-
|
|
144
|
+
All private instance methods can only be called with an implicit receiver (implicit self) in Ruby.
|
|
127
145
|
|
|
128
|
-
That's why things like this work, because were always "inside" an object:
|
|
146
|
+
That's why things like this work with an implicit receiver/self, because were always "inside" an object:
|
|
129
147
|
|
|
130
148
|
```ruby
|
|
131
149
|
puts self
|
|
132
150
|
# main
|
|
133
151
|
# nil
|
|
134
152
|
|
|
135
|
-
# implicit receiver for puts
|
|
153
|
+
# implicit receiver/self for puts
|
|
136
154
|
puts "foo"
|
|
137
155
|
```
|
|
138
156
|
|
|
139
|
-
but this doesn't:
|
|
157
|
+
... but this doesn't, since we called `puts` on an explicit receiver:
|
|
140
158
|
|
|
141
159
|
```ruby
|
|
142
|
-
# explicit receiver for puts
|
|
160
|
+
# explicit receiver/self for puts
|
|
143
161
|
self.puts "foo"
|
|
144
162
|
# => NoMethodError: private method `puts' called for main:Object
|
|
145
163
|
```
|
|
146
164
|
|
|
147
|
-
So if you want to provide functionality that's available everywhere like `puts
|
|
165
|
+
So if you want to provide functionality that's available everywhere like `puts`, but cannot be called on an object, the usual approach is to define a private instance method on Kernel.
|
|
166
|
+
|
|
167
|
+
That's what has been done with the `piper` and `p_end` methods and can be seen in the source [here](https://github.com/christophweegen/pied-piper/blob/master/lib/pied_piper/kernel.rb) :-)
|
|
168
|
+
|
|
169
|
+
Just a bit of background information, in case you're curious how this gem was implemented.
|
|
148
170
|
|
|
149
|
-
|
|
171
|
+
But back to usage...
|
|
150
172
|
|
|
151
173
|
### Array Pipes
|
|
152
174
|
|
|
@@ -161,6 +183,26 @@ p | concat | p.end
|
|
|
161
183
|
# => "Pied Piper of Hamelin"
|
|
162
184
|
```
|
|
163
185
|
|
|
186
|
+
If the first array element is a Symbol and the last is an object of class `Proc`, we can also use methods which accept blocks:
|
|
187
|
+
|
|
188
|
+
```ruby
|
|
189
|
+
p = piper("Pied Piper")
|
|
190
|
+
|
|
191
|
+
map_double = [:map, ->(str) { str * 2 }]
|
|
192
|
+
p | :split | map_double | :join | p_end
|
|
193
|
+
# => "PiedPiedPiperPiper"
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
It's also possible to use methods with arguments and blocks, arguments have to between the method name (first element of the array) and the last element of the array which is a block:
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
p = piper("Pied Piper")
|
|
200
|
+
|
|
201
|
+
map_double_array = [:each_with_object, [], ->(str, array) { |str| array << [str * 2]}]
|
|
202
|
+
p | :split | map_double_array | p_end
|
|
203
|
+
# => [["PiedPied"], ["PiperPiper"]]
|
|
204
|
+
```
|
|
205
|
+
|
|
164
206
|
### Proc Object Pipes
|
|
165
207
|
|
|
166
208
|
An Object of `Proc` class which takes exactly one parameter:
|
data/lib/pied_piper.rb
CHANGED
|
@@ -26,12 +26,18 @@ class PiedPiper
|
|
|
26
26
|
when Symbol
|
|
27
27
|
@object.send(function)
|
|
28
28
|
when Array
|
|
29
|
-
|
|
30
|
-
case
|
|
29
|
+
method, *args, blk = function
|
|
30
|
+
case method
|
|
31
|
+
|
|
31
32
|
when Symbol
|
|
32
|
-
|
|
33
|
+
case blk
|
|
34
|
+
when Proc
|
|
35
|
+
@object.send(method, *args, &blk)
|
|
36
|
+
else
|
|
37
|
+
@object.send(method, *args, blk)
|
|
38
|
+
end
|
|
33
39
|
when Method
|
|
34
|
-
|
|
40
|
+
method.call(@object, *args)
|
|
35
41
|
end
|
|
36
42
|
when Proc
|
|
37
43
|
function.call(@object, *args)
|
data/lib/pied_piper/version.rb
CHANGED