# ANN: Matchable 0.2

**URL:** <https://www.ruby-forum.com/t/ann-matchable-0-2/59035>\
**Category:** Ruby\
**Created:** [April 28, 2006, 1:07am UTC](https://www.ruby-forum.com/t/ann-matchable-0-2/59035 "2006-04-28T01:07:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [April 28, 2006, 1:07am UTC](https://www.ruby-forum.com/t/ann-matchable-0-2/59035/1 "2006-04-28T01:07:40Z")

</div>

[http://matchable.rubyforge.org/](http://matchable.rubyforge.org/)

The Matchable mixin borrows ideas from ML  
[http://www.smlnj.org/sml97.html](http://www.smlnj.org/sml97.html)(and  
probably other functional languages).  
The syntax currently supported looks like this:

_class_ Bar  
_include_ Matchable  
_def\_match_(:plus, _nil_, _nil_){|a,b| a+b}  
_def\_match_(:plus, String, _nil_){|a,b| “#{a}:=\>#{b}”}  
_def\_match_(:plus, /hello/, _nil_){|a, b| “hello world”}  
_end_

b = Bar.new  
=\> _#Bar:0xb758e7ac_  
b._plus_(5,5)  
=\> 10  
b._plus_(“five”,5)  
=\> “five:=\>5”  
b._plus_(“hello”, 5)  
=\> “hello world”

_class_ Foo  
_include_ Matchable  
_def_ _test_(tuple)  
_case_ tuple  
_when_ _mcase_([:test, 5, 5]) _then_ “poop”  
_when_ _mcase_([:test, _nil_, _nil_]) _then_ “whoop”  
_when_ _mcase_([_nil_, _nil_]) _then_ “hoot”  
_else_ “foot”  
_end_  
_end_  
_end_
