Folks,
I’ve used Austin’s complete response, because it seems complete and is well
organised. It may be a bit too much information for the FAQ, but it’s good for
now. Eventually, I hope that information like this can be included in the Ruby
distribution.
The operators in the text below are not universally quoted, so I’ll work on
that, but can anybody pick any more holes?
Anyway, here it is.
···
Q. What are the non-alphanumerical symbols in Ruby code?
A.
<=> <= < >= > == !=
1. Relational operators, used to determine the relative equality
of values. All of these symbols, excepting "!=", are defined
as methods on classes. The most common way of implementing
these values is through the [Comparable] mixin, where it is
only necessary to define "<=>" (which returns -ve, 0, or +ve
to indicate comaprison). All other relational operators, if
defined manually, must return a boolean value (either true or
false). The "!=" operator is special syntax in the
interpreter, such that "a != b" really means "!(a == b)".
2. The "<" operator also means class inheritance, such that in
the following example, a Car inherits from Vehicle.
class Car < Vehicle; end
-
-
- / % **
-
Binary arithmetic operators representing addition (3 + 2),
subtraction (3 - 2), multiplication (3 * 2), division (3 /
2), modulus (integer division remainder; 3 % 2 is 1), and
exponentiation (3 ** 2 is 9). “**” has the highest
precedence, followed by “*”, “/”, and “%”, and finally "+"
and “-”. All of these operators are methods. -
Unary plus and minus (+3, -2). Unary plus and minus have
higher precedence than “*”, “%”, and “/”, but lower precedence
than “**”. These are methods (+@ and -@, respectively).
-
~ << >> & ^ |
1. "<<" most often means that a "here" document follows, e.g.:
puts <<EOS
This is a here document.
EOS
2. "<<" also creates a new class based on a particular object.
These object-specific classes are called 'singleton classes'.
In the example below, only the Car object referred to by
"camaro" will have the method "has_flames?".
camaro = Car.new
class << camaro
def has_flames?
true
end
end
3. Binary (bitwise) operators. "~" returns the bitwise
complement of the value (~2 is -3); "<<" and ">>" shift the
bits of the value left and right by the specified number of
bits (2 << 2 is 8 >> 2). "&", "^", and "|" are bitwise "and"
and exclusive and regular "or" operators.
===
Case comparison operator. Used to select branches in case
statements. In the following code, "/pattern/ === string" is
implicitly called.
case string
when /pattern/ then ...
end
… …
Range defnitions. ".." ranges are end-inclusive; "..." ranges are
end-exclusive. Examples:
0...10
'a'..'z'
: ?: ::
1. :name is a Symbol. Most often used as a parameter to various
methods in the Module class, (e.g., public, private,
protected, attr_reader, attr_accessor, etc.). Symbols are
immutable, immediate value strings.
2. ?: is the ternary operator:
message = error ? error.to_s : "Everything's fine"
3. :: resolves scope. ::Foo refers to a constant Foo at the top
level; Net::Ftp refers to a constant Ftp in the module (or
class) Net. In this case, the constant Ftp is another class.
[]
1. Array definition, as in:
ndays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]
2. References an element of a collection - in particular, arrays
and hashes. This is a method call, defined as [](x).
ndays[1] == 28
ndays[-3] == 31
{} =>
Hash definition, most often used with "=>". Can be used in one of
two ways. The examples below will result in the same key/value
pairs. "=>" explicitly relates a key to a value; if => is not
used, then pairs are joined. An odd number of values will throw
an error, regardless of which method is used. The two methods may
not be mixed.
{ 1, 2, 3, 4, 5, 6 }
{ 1 => 2, 3 => 4, 5 => 6 }
!
Logical negation. Turns "false" to "true" and "true" to "false".
method? method!
An optional but useful naming scheme for some methods. The symbol
is actually part of the method name. In general:
method! changes the object (e.g. String.gsub!)
method? returns boolean (e.g. Array.empty?)
=~ !~ // %r{}
Regular expression handling. "=~" is a method, "!~" is special
syntax for "!(=~)", like "!=". // and %r{} have the same effect
in defining a regular expression. %r{} can be represented with
other characters. All of the following represent the same regular
expression. Note that if the first character following "%r" is
part of a matching pair ((), <>, [], {}), then the second
character must be the matching character; otherwise, the two
characters must be the same.
/\d+/
%r{\d+}
%r/\d+/
%r@\d+@
%r(\d+)
#{}
String expression substitution. #{expr} substitutes the value of
expr in a double-quoted string (including regular expressions and
command strings). This can only be done with {}.
%q{} %Q{} %{}
Quotes, expressed another way. %q{} is equivalent to '' and has
all the same semantics (no substitution excepting \\ to \ and \'
to '). %Q{} and %{} are equivalent to "" and have the same
semantics (full substitution, including expression substitution
with #{}). As with regular expressions, the {} in %q{} or %Q{}
can be represented with other characters.
%w{}
This will create an array from the string within by splitting
the string on spaces. The string is treated as a single-quoted
string.
dayabbrev = %w[Sun Mon Tue ...]
dayabbrev == ["Sun", "Mon", "Tue", "..."]
%x{} ``
A command string. Try this in irb:
files = `ls` # Unix
files = `dir /w` # Windows