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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d579aeffae1f032f9ee5e820ddbbaa907cdf9126aa053dbbd97d7d31e38cc24b
4
- data.tar.gz: ca5279f4ddbf50304fa72029a5f3d871beb79f2e90b16f67da14d6bc19a723e4
3
+ metadata.gz: 28891debbf93a2a534057564d68b0fa12eb56a320634e495d084efe62df74651
4
+ data.tar.gz: 5f77d34e71bf2e0ec423d967dd7eb92edf2012f28cad208cb802e3848040d009
5
5
  SHA512:
6
- metadata.gz: 0c210a151538ab94ef386fd7fa463007545b4a2329b67e59c717c5b2055b9abb854ea661034b6190db1f5965b2d63d238bd783377ba7cc641425df704986de5b
7
- data.tar.gz: 9a98bd3b029ff085f5d96cc7c1ffa1a915b3e64ec1a3b9d51069597d1c18dd2180a0e7f31049e2faa58129b6ca2602d9b8bb76d854d34ad925eccbf7f6c93f23
6
+ metadata.gz: 9b686c4a5412660cae523bd19d9ca76aae68764680801d1ec8bf742928236a07c14a3f7df8e49a454f3ced3b6178e60fa0386125b3565ae5d24c9d2e6a61303a
7
+ data.tar.gz: a4a1c639b8c1abcb819ca4990b6161189190c84e3e29bc9c26cbcf951c0c5b86c497a9df35dcce0cf96859fff085c2cc657eff8b4f3f0abdf3d0d9ed208566fc
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in pied_piper.gemspec
6
6
  gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- pied_piper (0.1.4)
4
+ pied_piper (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
- Private instance methods can only be called with an implicit receiver (implicit self) in Ruby.
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` the usual approach is to define a private instance method on Kernel.
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
- That's what has been done with the `piper` and `p_end` method :-)
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:
@@ -26,12 +26,18 @@ class PiedPiper
26
26
  when Symbol
27
27
  @object.send(function)
28
28
  when Array
29
- meth, *args = function
30
- case meth
29
+ method, *args, blk = function
30
+ case method
31
+
31
32
  when Symbol
32
- @object.send(meth, *args)
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
- meth.call(@object, *args)
40
+ method.call(@object, *args)
35
41
  end
36
42
  when Proc
37
43
  function.call(@object, *args)
@@ -1,3 +1,3 @@
1
1
  class PiedPiper
2
- VERSION = '0.1.4'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pied_piper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Weegen