29

Is there a list anywhere of all ruby operators that can be overridden? (Not the ones that can't!)

3
  • 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. Sep 30, 2013 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 7, 2013 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. Oct 8, 2013 at 0:47

1 Answer 1

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.

2
  • 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 3, 2012 at 4:14
  • 1
    Actually, != 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. Dec 29, 2012 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.