Chains
Requiring Chains
Chains can be used by simply requiring it, and calling chain and with methods:
require 'chains'
chain applies the operations in the block to the object, returning the result of the last expression:
str = 'Hello'
Chains::chain(str, -> {
concat(' World')
length
})
# => 11
with applies the operations in the block to the object, and returns the object:
str = 'Hello'
Chains::with(str, -> {
concat(' World')
length
})
# => 'Hello World'
Including Chains
Chains can be included into a class to add the chain and with methods to the object.
require 'chains'
class SuperString < String
include Chains
end
Then:
obj = SuperString.new
obj.chain(-> {
concat(' World')
length
})
# => 11
obj = SuperString.new
obj.with(-> {
concat(' World')
length
})
# => 'Hello World'