Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Does Ruby Only Permit Certain Operator Overloading

In Ruby, like in many other OO programming languages, operators are overloadable. However, only certain character operators can be overloaded.

This list may be incomplete but, here are some of the operators that cannot be overloaded:

!, not, &&, and, ||, or
like image 933
Tony Pitale Avatar asked Sep 18 '08 14:09

Tony Pitale


3 Answers

"The && and || operators are not overloadable, mainly because they provide "short circuit" evaluation that cannot be reproduced with pure method calls."

-- Jim Weirich

like image 51
Joe Van Dyk Avatar answered Nov 03 '22 04:11

Joe Van Dyk


Methods are overloadable, those are part of the language syntax.

like image 27
Farrel Avatar answered Nov 03 '22 05:11

Farrel


Yep. Operators are not overloadable. Only methods.

Some operators are not really. They're sugar for methods. So 5 + 5 is really 5.+(5), and foo[bar] = baz is really foo.[]=(bar, baz).

like image 12
Jordi Bunster Avatar answered Nov 03 '22 05:11

Jordi Bunster