Canal
Canal is an utility that builds callable objects out of a chain of method calls. It can be used to write simpler and shorter code that avoids using blocks or lambdas.
Usage
Include the canal
gem and expose the canal
method:
require 'canal'
Using canals instead of lambdas
lambda { |x| x.to_s(2) }
canal.to_s(2)
Using canals instead of blocks
[1, 2, 3].map { |value| value + 1 }
[1, 2, 3].map(&canal + 1)
[1, 2, 3].select { |value| value > 1 }
[1, 2, 3].select(&canal > 1)
Using hashes
people = [{ name: "Alice" }, { name: "Bob" }]
people.map(&canal[:name])
=> ["Alice", "Bob"]
Nesting canals
# Format a 2D array into a grid
rows = [[1, 2], [31, 4]]
puts rows.map(&canal.map(&canal.to_s.ljust(3, ' ')).join).join("\n")
1 2
31 4
Installation
Add this line to your application's Gemfile:
gem 'canal'
And then execute:
$ bundle
Or install it yourself as:
$ gem install canal
Contributing
- Fork it ( http://github.com/becojo/canal/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