Wed 14 Jun 2006
Unary vs. Binary Operators
Posted by dkaz under Ruby, Programming
Here’s another (oh so simple) definition that somehow escaped through the cracks of my brain over time. I’m blushing over this one.
Operators are binary or unary. Binary operators (”bi” as in “two”) have two operands. In “A*B” the * operator has two operands: A and B. In “!B” the “!” operator (meaning boolean NOT) has only one operand, and is therefore a unary operator. The “-” and “+” operators can be both binary and unary, in “-4″ or “+4″ it denotes a negative or a positive number, in “0-4″ it acts as the subtraction operator.
Why’s smooth meta-programming hack is fun to play with and is responsible for exposing gaps in my unary expertise. His code overloads Ruby’s unary ops (-@, +@) creating a clean DSL syntax w/o using any eval hacks. Good stuff.
