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;
end
 
def if2 cond1, cond2
  if cond1 and cond2
    yield
    HopelesslyEgocentric.new
  elsif cond1
    Class.new(HopelesslyEgocentric) do
      def else1; yield; HopelesslyEgocentric.new; end
    end.new
  elsif cond2
    Class.new(HopelesslyEgocentric) do
      def else2; yield; HopelesslyEgocentric.new; end
    end.new
  else
    Class.new(HopelesslyEgocentric) do
      def neither; yield; end
    end.new
  end
endThen 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" endI thought that was pretty nifty. I doubt performance is amazing, but it is fairly easy to use!

No comments:
Post a Comment