From: Austin Ziegler Date: 2002-12-02T01:42:31+09:00 Subject: Re: [FAQ] What do some of Ruby's symbols mean? On Sun, 1 Dec 2002 23:07:25 +0900, Gavin Sinclair wrote: > This could do with some community input before going to the FAQ. > The format will be different but I just wanted to bang out some > content. I haven't even checked if there is a question like this. > > Can anyone think of a question title that does't create confusion > with :name symbols? [FAQ] What do you mean, <=!@/$!? Half tongue in cheek. > Q: What do some of Ruby's symbols mean? > > A: This question is about non-aplhanumeric characters in Ruby > code, not about *symbols* like :name. For those, see [appropriate > link]. Actual explanations of the concepts behind them is beyond > the scope of this answer. [Note: As I am defining these, I am going from most frequent to least frequent use for the various default definitions.] <=>, <=, <, >=, >, ==, != 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 must return a value of -1, 0, or +1). All other relational operators, if defined manually, must return a boolean value (either FalseClass or TrueClass). 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 +, -, *, /, %, ** 1. Binomial 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. 2. 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 <>" 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. ., ... Range defnitions. ".." ranges are end-inclusive; "..." ranges are end-exclusive. See [Range]. :, ?:, :: 1. Symbol mark. This marks the name as a Symbol, such that :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: (boolean-test) ? (do-if-true) : (do-if-false) 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. [] 1. References an element of a collection. This is a method call, defined as "[](x)". Defining this method also gets "[]=" for free. 2. Array definition, as in: ndays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] {}, => Hash definition, most often used with "=>". Can be used in one of two ways. The example 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". =~, !~, //, %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. %x{} A command string. This behaves the same as backtick (``) command execution. -austin -- Austin Ziegler, austin@halostatue.ca on 2002.12.01 at 10.18.42