Module: Pipeline
- Defined in:
- lib/pipeline.rb
Overview
The Pipeline
Refinement module bundles a method duo that builds
a clean and clever pure-Ruby solution to rightward method piping.
Check out the README for examples.
Reminder: activate a Refinement module with using
:
using Pipeline
my_obj.then_pipe(…)
Refinements are only active for the module/class block or file
(if top-level) that’s using
them. See: refinements.rdoc
(on docs.ruby-lang.org)
Instance Method Summary collapse
-
#`(name) ⇒ Object
Alias
Object#method
. -
#sys(command) ⇒ Object
Provide a replacement alias for
Kernel#`
, the subshell method. -
#then_pipe(*procs) ⇒ Object
Yield
self
to the firstProc
(or#to_proc
object) argument, then the result to the second argument, and so forth.
Instance Method Details
#`(name) ⇒ Object
Alias Object#method
.
m = 42.` :to_s
m.call #=> "42"
The `
method is the backend to the `…`
and %x{…}
syntaxes.
class MyArray < Array
using Pipeline
def fetch_values(*indices)
indices.map(&`[]`)
end
end
a = MyArray.new(['A', 'B', 'C'])
a.fetch_values 1, 3 #=> ["B", nil]
49 50 51 52 53 54 |
# File 'lib/pipeline.rb', line 49 module Pipeline; refine Object do def then_pipe(*procs) = procs.reduce(self) { _1.then(&_2) } alias sys ` alias ` method end end |
#sys(command) ⇒ Object
Provide a replacement alias for Kernel#`
, the subshell method.
49 50 51 52 53 54 |
# File 'lib/pipeline.rb', line 49 module Pipeline; refine Object do def then_pipe(*procs) = procs.reduce(self) { _1.then(&_2) } alias sys ` alias ` method end end |
#then_pipe(*procs) ⇒ Object
Yield self
to the first Proc
(or #to_proc
object) argument,
then the result to the second argument, and so forth.
construct_url(arguments).then_pipe(
proc {|url| URI(url).read },
JSON.public_method :parse
)
49 50 51 52 53 54 |
# File 'lib/pipeline.rb', line 49 module Pipeline; refine Object do def then_pipe(*procs) = procs.reduce(self) { _1.then(&_2) } alias sys ` alias ` method end end |