matchrb
matchrb (pronounced 'matcherby') provides a simple but powerful way to do pattern matching in Ruby.
Installation
Add this line to your application's Gemfile:
gem 'matchrb'
And then execute:
$ bundle
Or install it yourself as:
$ gem install matchrb
Usage
matchrb implements two methods, #match and #otherwise. You can use them
globaly like so:
require 'matchrb/global'
object = 100
match object,
String => "object is a string",
Integer => "object is an integer",
otherwise => "object is...just an object"
or, to avoid name clashes, use them individually:
require 'matchrb'
object = 100
Matchrb.match object,
String => "object is a string",
Integer => "object is an integer",
Matchrb.otherwise => "object is...just an object"
That's it!
Contributing
- Fork it ( https://github.com/dlisboa/matchrb/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 a new Pull Request