0x16 – It’s a Bird…It’s a Plane…It’s Ruby!

Ever done Erlang? Now I wondered how flexible ruby would be and if an erlang style pattern matcher can be done in ruby. As it turns out, I came pretty close:

You might recognize this:


m = matching! [ 1, [ :a, 2 ], 3 ] do
  on [ a ]                do puts [ "1", a ].inspect end
  on [ a, _, a ]          do puts [ "2", a ].inspect end  
  on [ a, [ :a, 3 ], z ]  do puts [ "3", a, z ].inspect end  
  on [ a, [ b, 2 ], z ]   do puts [ "4", a, b, z ].inspect; self.z = :z end
  on [ a, [ :a, 2 ] , z ] do puts [ "5", a, z ].inspect end
end

Even if not, I bet you understand what it is supposed to do; if you don’t: the output is [“4”, 1, :a, 3].

How this all works? Easy: start off a BlankSlate, add some method_missing magic, off you go! All in under 100 lines.

Note to wordpress: This sucks, really sucks. Limited to a numbr of 25 or so templates sucks, and then each comes with its own limitations. I guess I have to go somewhere else…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s