Forum: Ruby ANN: Matchable 0.2

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mark Alexander F. (Guest)
on 2006-04-28 03:07
(Received via mailing list)
http://matchable.rubyforge.org/

The Matchable mixin borrows ideas from ML
<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*
This topic is locked and can not be replied to.