14.3. Operators
Regardless of the language, there is usually some commonality. There's addition, subtraction, multiplication, division, and assignment. In some languages, including Ruby and JavaScript, the addition operator does double duty as the concatenation operator. This means that examples such as the following are pretty much the same, regardless of the language:
X = 1 + 1
X = 1 1
X = 1 * 1
X = 1 / 1
However, occasionally will you see something a little out of the ordinary, usually in languages that borrow some of their syntax from C. In Ruby, they're called multiple assignments; I like to think of them as less typing. Consider, for a moment, the following line of code:
All that it does is increment the variable X by 5, so wouldn't it be easier to type this instead?
Yeah, all that I'm saving is two keystrokes, the second X and a space, but it adds up. Imagine for a moment the variable name was my last name, Woychowsky instead of X. Having to type it only once would greatly extend the life of the W key. The same shortcut is available for subtraction, multiplication, and division.
|