On Nov 20, 2009, at 11:27 AM, Markus wrote:
> As for the main point, I've been known to write:
>
> class String
> def |(other)
> strip.empty? ? other : self
> end
> end
>
> Which can be used like so:
>
> puts Product.name | '(no name supplied)'
Interesting. On the one hand, I like that this reads like the inbuilt
|| operator, and is reasonably lightweight syntax. (So similar, in
fact, that a casual reader might not notice that there was anything
tricksy going on.) On the other hand, it would be very easy to type
this by mistake:
puts Product.name || '(no name supplied)'
...which, precisely *because* it reads so similarly, would be
difficult to detect (especially by another programmer who hadn't
spotted your '|' trick, and who would then think you were a complete
moron).
Oh, and now we're introducing another set of behavior for the |
operator, which also already means:
- non-short-circuiting "or" (on true, false, and nil),
- bitwise "or" (on Bignums),
- set union (on Array).
...which -- while arguably Not That Bad in this instance -- is
starting to mosey slightly Perl-ward. "If you're in scalar^H^H^H
string context, this operator does foo..."
On balance, though, I think I like it. (=
-Sam