Think riparian. Think old man river, he jus' keep on rollin'. Think rive. Also river, reaver, repair, reaper.
Tear chained method calls apart, put them in a block, and return the block value. eg
Before:
result = values.select{|x| x.name =~ /Jo/}.map{|x| x.count}.inject(0){|s,x| s + x}
After:
result = values.rive do
select{|x| x.name =~ /Jo/}
map{|x| x.count}
inject(0){|s,x| s + x}
end
This is also a little different to instance_eval, because the following will work:
outside_block_regex = /Wahoody-hey/
result = values.rive do
select{|x| x.name =~ outside_block_regex}
map{|x| x.count}
inject(0){|s,x| s + x}
end
Warning this can have some rare but weird side effects:
- will probably break on classes that have defined method_missing, but not respond_to_missing
- an outside variable with the same name as an inside method taking in-place hash argument will cause a syntax error.
But you can obviate all of that by just using the safe syntax:
outside_block_regex = /Wahoody-hey/
result = values.rive do |vs|
vs.select{|x| x.name =~ outside_block_regex}
vs.map{|x| x.count}
vs.inject(0){|s,x| s + x}
end
Or using the magic disambiguaters:
select = /Wahoody-hey/
result = values.rive do
__inside__.select{|x| x.name =~ __outside__.select}
map{|x| x.count}
inject(0){|s,x| s + x}
end
Installation
Add this line to your application's Gemfile:
gem 'ripar'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ripar
Usage
In your class
class YourChainableThing
include Ripar
end
yct = YourChainableThing.new.rive do
# operations
end
In a singleton
o = Object.new
class << o
include Ripar
end
The mostest lightweightiest
o = Object.new.extend(Ripar)
Monkey-patch
class Object
include Ripar
end
Contributing
The standard github pull request dance:
- Fork it ( http://github.com//ripar/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
PS
Thing = Struct.new :name, :count
values = [
Thing.new('John', 20),
Thing.new('Joe', 7),
Thing.new('Paul', 3),
Thing.new('James', 3),
Thing.new('Wahoody-heydi-dude', 3.141527),
]