From: Paul Brannan Date: 2002-04-23T05:44:32+09:00 Subject: Re: Ruby as Smalltalk Oops, a small bug in the code I posted produces: p false.ifFalse { true }.ifTrue { 42 }.endIf #=> 42 The following should fix it: class Object class Invalid; end def ifTrue(x=Invalid); IfProxy.new(yield, realSelf(x)); end def ifFalse(x=Invalid); realSelf(x); end def endIf; self; end def realSelf(x); (x == Invalid) ? self : x; end end class FalseClass def ifTrue(x=Invalid); realSelf(x); end def ifFalse(x=Invalid); IfProxy.new(yield, realSelf(x)); end end class NilClass def ifTrue(x=Invalid); realSelf(x); end def ifFalse(x=Invalid); IfProxy.new(yield, realSelf(x)); end end class IfProxy def initialize(x, t); @x = x; @t = t; end def ifTrue(x=Invalid, &b); @t.ifTrue(@x, &b); end def ifFalse(x=Invalid, &b); @t.ifFalse(@x, &b); end def endIf; @x; end end (Unfortunately, it's not quite the same as the real if/then/else/end, since it doesn't return nil if the condition is false and you don't supply an else). Would anyone care to golf with this? Paul