In Ruby, `1 + 2` looks like an arithmetic expression, but `+` is a method call: `1.+(2)`. Syntactic sugar is a convenient way to write certain calls without the usual `object.method(arguments)` form. The notation changes how a call looks, not which method receives it. section 7.2
Assignment-like syntax works the same way. `ticket.price = 63.00` calls `price=` on `ticket`, passing `63.00`. It does not assign to a variable named `ticket.price`. The method can do more than store the value: a price setter might reject an amount with extra decimal places, while a year setter might normalize a two-digit year. section 3.3.2 section 3.3.3
Give an object an operator
Define `+` as a method, and an instance can be used with `obj + 100`. Ruby calls `obj.+(100)`. The method’s behavior is yours to define, though a surprising meaning for `+` can mislead readers. Similarly, defining `-` lets an account object use `acc -= 5`: the shortcut stands for `acc = acc - 5`. section 7.2.1
This pattern extends beyond arithmetic to indexing, comparisons, case equality, and bitwise operations. Not every operator can be redefined: `||=` and `&&=` offer shortcut notation, but their underlying `||` and `&&` operators cannot be overridden. When an arithmetic operator behaves unexpectedly, inspect its method definition. section 7.2.1 section 8.3.2
Table 7.2 Methods with operator-style syntactic sugar–calling notation
| Category |
Name |
Definition example |
Calling |
Sugared |
|---|---|---|---|---|
| Arithmetic method/operators |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Get/set/append data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Comparison method/operators |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Case equality operator |
|
|
|
|
| Bitwise operators |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Compare ordinary method calls with their operator-style forms, including `obj.[](x)` and `obj[x]`. section 7.2.1

