Some small additions/comments to an article by Kevin Newton "Ruby operators"
%
- The modulo operator. This is usually only found on numeric types.
And Ranges :)
(1..30) % 5
# => ((1..30).%(5)) -- an ArithmeticSequence
!=
- The inequality operator. This is found on almost all types, and is used to check basic inequality.
Maybe deserves mentioning that it can be defined individually or, if not, (like !~
) breaks down to negating ==
.
===
- The case equality operator. This method is used as the backend for case statements.
Also grep
and any?
/all?
/none?
.
[]
- The element reference operator.
Maybe these usages deserve mentioning:
Array::[]
Proc#[]
Truthiness operators
My favorite pet peeve :) (1, 2)
I believe they deserve splitting into "boolean arithmetics" operators (&&
/||
) and "control flow" operators (and
/or
), and I don't think the latter deserve being described as "alias with no difference," as they have completely different precedence, and (as you most probably know :)) in allowed to use by parser in different contexts:
# allowed
foo = fetch_foo or return
@error = find_error and raise
# not allowed
foo = fetch_foo || return
@error = find_error && raise
(I believe "control flow and
/or
" were inherited from Perl.)
I would honestly say that &&
/||
deserve to be in their own section (boolean math), while and
/or
's deserved place in this "Control flow" section.
Also, it seems like case
is missing from "Conditional operators" section, or you don't consider it an "operator" (unlike if
; I am not sure if the if
and friends should be treated as "opertors" either, though).