From: Matthew Moss Date: 2006-01-26T06:26:32+09:00 Subject: Re: readable and provocative & daring On 1/25/06, Richard Drake wrote: > Yes, and it was the ability to extend & to String that started as a > 'what if?' this time last week and ended with some serious questions > about how wide the utility of this version may be. Or perhaps Ruby > veterans would vote it down for reasons I might not currently grasp? Well, if you simply wanted to create a & method on String, that's easy: class String def &(rhs) self + rhs end end If what you actually wanted is to make '&' short-circuit, that's a whole 'nother problem that, IMO, is best left alone. First, it flies in the face of the traditional meaning from other long-lived programming languages. (I'm very willing to discard or change tradition if it makes gains on efficiency, readability, etc... but I highly disagree that making '&' short-circuit is worth it, especially when Ruby has both '&&' and 'and'.) Second, it could not be (simply) implemented with a standard method call. The nature of calling a function is that its arguments are evaluated. Which means that 'rhs' in String.& above must be evaluated before the function can be called. So String.& cannot possibly short-circuit. (Related... If you read any good-programming-practices book for C++, they warn against overloading operator&& and operator|| for specifically this reason: they cannot short-circuit.) You could, possibly, redefine String.& like this: class String def &(rhs) some_cond ? self + rhs : self end end And then, if rhs is an object that lazily evaluates, you might be able to fake short-circuitness. But I think it's not worth it that much... smells bad, probably better ways to do things.