On 12/19/2013 06:04 AM, Joel Pearson wrote:
> I'm curious why you didn't implement this using this approach?
>
> class Object
>    def if
>      self ? yield : self
>    end
>
>    def unless
>      self ? self : yield
>    end
> end
>
> Ruby will automatically raise if you use yield without a block.
> I don't see any need to modify false and nil, since Ruby can already
> test them using "self".
>
> Am I missing something else here?

Very slightly simpler...

class Object
   def if
     self and yield
   end

   def unless
     self or yield
   end
end

true.if { puts "ok" }  # =>> ok
false.if { puts "fail" }

true.unless { puts "fail" }
false.unless { puts "ok" }  # =>> ok