Is there a list anywhere of all ruby operators that can be overridden? (Not the ones that can't!)
Asked
Active
Viewed 8,293 times
29
-
**Overloading** is the use of the same method name for different actions, where the method is only distinguishable by its signature. Actually that is not possible in Ruby. Overriding or redefining is the proper term here. – Konrad Reiche Sep 30 '13 at 20:01
-
Thanks for the detail, @platzhirsch. I've edited the question. Strange how it's generally called operator overloading then, no? – aaaidan Oct 07 '13 at 23:29
-
Well one, like me, can always be picky with respect to use of terms and their meaning :-) After all it is derived from languages like C++ where it maybe does the same, but is based on different semantics. – Konrad Reiche Oct 08 '13 at 00:47
1 Answers
38
Here's a table of the Ruby operators.
The ones that are methods and overloadable are:
[ ] [ ]= Element reference, element set
** Exponentiation
! ~ + - Not, complement, unary plus and minus (method names for the last two are +@ and -@)
* / % Multiply, divide, and modulo
+ - Plus and minus
>> << Right and left shift
& Bitwise `and'
^ | Bitwise exclusive `or' and regular `or'
<= < > >= Comparison operators
<=> == === != =~ !~ Equality and pattern match operators (!= and !~ may not be defined as methods)
The table was from the 2001 Pickaxe book, but that's the same table as in the Ruby 1.9 Pickaxe book -- no reason to believe that this set of infix operators will ever change.

Mark Rushakoff
- 249,864
- 45
- 407
- 398
-
By "the ones that are methods and overloadable," I assume you mean one can only overload the operators that are methods, no? Also, while "pure" operators cannot be overloaded, one can abuse the more word-like keywords (e.g. `defined?`, `not`, `or`, `begin`) as names for new methods without any infixy goodness. – fny Jun 03 '12 at 04:14
-
1Actually, != and !~ can be overloaded/defined, even though the old pickaxe says you can't. Back in 2008 there was a complaint about it on the mailing list and Matz commented that Ruby was flexible. – Anthony Michael Cook Dec 29 '12 at 06:17