Mar 10, 2011


I was browsing on Rosetta Code this evening in an attempt to find some unfinished tasks to fiddle with, when I found a rather interesting example. The task is to create a new "if2" statement that takes two conditional statements rather than one. In addition to the regular "if" block, it takes three "else" statements that are executed depending on which of the conditions are true. I've actually had this situation come up when I've been programming and at the time it never occurred to me that I could extend Ruby to allow for this scenario.

It turns out you can! It takes a little bit of combinator and anonymous class hackery but it does the job:
class HopelesslyEgocentric
  def method_missing what, *args; self; end;
def if2 cond1, cond2
  if cond1 and cond2
  elsif cond1 do
      def else1; yield;; end
  elsif cond2 do
      def else2; yield;; end
  else do
      def neither; yield; end
Then you can go ahead and use it like this:
if2(5 < x, x < 7) do
  puts "both true"
end.else1 do
  puts "first is true"
end.else2 do
  puts "second is true"
end.neither do
  puts "neither is true"
I thought that was pretty nifty. I doubt performance is amazing, but it is fairly easy to use!

No comments: