Forum: Ruby ANN: Matchable 0.2

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

The Matchable mixin borrows ideas from ML
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"}

b =
=> *#<Bar:0xb758e7ac>*
=> 10
=> "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"
This topic is locked and can not be replied to.