Expressions

From Ruby Standard Wiki

Jump to: navigation, search

Contents

11 Expressions

11: Expressions

Syntax

expression ::
    keyword-logical-expression

Semantics

See §11.1 for keyword-logical-expression

11.1 Logical Expressions

11.1: Logical Expressions

Syntax

keyword-logical-expression ::
    keyword-NOT-expression
  | keyword-AND-expression
  | keyword-OR-expression
keyword-NOT-expression :: method-invocation-without-parentheses | operator-expression | logical-NOT-with-method-invocation-without-parentheses | not keyword-NOT-expression
logical-NOT-expression ::= logical-NOT-with-method-invocation-without-parentheses | logical-NOT-with-unary-expression
logical-NOT-with-method-invocation-without-parentheses :: ! method-invocation-without-parentheses
logical-NOT-with-unary-expression :: ! unary-expression
keyword-AND-expression :: expression and keyword-NOT-expression
keyword-OR-expression :: expression or keyword-NOT-expression
logical-OR-expression :: logical-AND-expression | logical-OR-expression || logical-AND-expression
logical-AND-expression :: equality-expression | logical-AND-expression && equality-expression

Semantics

A logical-NOT-expression or a keyword-NOT-expression of the form not keyword-NOT-expression is evaluated as follows:

  1. If it is of the form not keyword-NOT-expression, evaluate the keyword-NOT-expression. Let X be the resulting value.
  2. If it is a logical-NOT-expression, evaluate its method-invocation-without-parentheses or unary-expression. Let X be the resulting value.
  3. If X is a true value, the value of the keyword-NOT-expression or the logical-NOT-expression is false.
  4. Otherwise, the value of the keyword-NOT-expression or the logical-NOT-expression is true.

Instead of the above process, a conforming processor may evaluate a logical-NOT-expression as follows:

  1. Evaluate the unary-expression or the method-invocation-without-parentheses. Let V be the resulting value.
  2. Create an empty list of arguments L. Invoke the method !@ on V with L as the list of arguments. The resulting value is the value of the logical-NOT-expression.

In this case, the processor shall:

  • include the operator !@ in operator-method-name.
  • define an instance method !@ in the class Object or one of its superclasses, if any. The method !@ shall not take any arguments. The method !@ shall return true if the receiver is false or nil, and shall return false otherwise.

A logical-AND-expression of the form logical-AND-expression && equality-expression or a keyword-AND-expression is evaluated as follows:

  1. Evaluate the expression or the logical-AND-expression. Let X be the resulting value.
  2. If X is a true value, evaluate the keyword-NOT-expression or equality-expression. Let Y be the resulting value. The value of the keyword-AND-expression or the logical-AND-expression is Y.
  3. Otherwise, the value of the keyword-AND-expression or the logical-AND-expression is X.

A keyword-OR-expression or a logical-OR-expression of the form logical-OR-expression || logical-and-expression is evaluated as follows:

  1. Evaluate the expression or the logical-OR-expression. Let X be the resulting value.
  2. If X is a false value, evaluate the keyword-NOT-expression or the logical-AND-expression. Let Y be the resulting value. The value of the keyword-OR-expression or logical-OR-expression is Y.
  3. Otherwise, the value of the keyword-OR-expression or logical-OR-expression is X.

11.2 Method invocation expressions

11.2: Method invocation Expressions

Syntax

primary-method-invocation ::
    super-with-optional-argument
  | indexing-method-invocation
  | method-only-identifier
  | method-identifier ( [ no whitespace here ] argument-with-parentheses )? block?
  | primary-expression [ no line-terminator here ] . method-name ( [ no whitespace here ] argument-with-parentheses )? block?
  | primary-expression [ no line-terminator here ] :: method-name [ no whitespace here ] argument-with-parentheses block?
  | primary-expression [ no line-terminator here ] :: method-name-without-constant block?
indexing-method-invocation :: primary-expression [ no line-terminator here ] optional-whitespace? [ indexing-argument-list ? ]
optional-whitespace :: [ whitespace here ]
method-name-without-constant :: method-name but not constant-identifier
method-invocation-without-parentheses :: command | chained-command-with-do-block | chained-command-with-do-block ( . | :: ) method-name argument | return-with-argument | break-with-argument | next-with-argument
command :: super-with-argument | yield-with-argument | method-identifier argument | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument
chained-command-with-do-block :: command-with-do-block chained-method-invocation*
chained-method-invocation :: ( . | :: ) method-name | ( . | :: ) method-name [ no whitespace here ] [ lookahead \notin { { } ] argument-with-parentheses
command-with-do-block :: super-with-argument-and-do-block | method-identifier argument do-block | primary-expression [ no line-terminator here ] ( . | :: ) method-name argument do-block

The primary-expression of a primary-method-invocation, command, and indexing-method-invocation shall not be a jump-expression.

If the argument-with-parentheses of a primary-method-invocation occurs, and the block-argument of the argument of the argument-with-parentheses occurs, the block of the primary-method-invocation shall not occur.

If the argument of a command-with-do-block occurs, and the block-argument of the argument-in-parentheses of the argument (see §11.2.1) occurs, the do-block of the command-with-do-block shall not occur.

The optional-whitespace of an indexing-method-invocation shall not occur if its primary-expression is any of the following construct:

  • A primary-method-invocation of the form method-only-identifier, method-identifier, primary-expression . method-name, or primary-expression :: method-name-without-constant
  • A method-invocation-without-parentheses of the form chained-command-with-do-block which satisfies all of the following conditions:
    1. Let M be the chained-command-with-do-block. One or more chained-method-invocation of M occurs.
    2. Let I be the last chained-method-invocation of M, in the order they appear in program text. I is of the form (.|::) method-name.

Semantics

A primary-method-invocation is evaluated as follows:

  1. If the primary-method-invocation is a super-with-optional-argument or an indexing-method-invocation, evaluate it. The resulting value is the value of the primary-method-invocation.
    • If the primary-method-invocation is a method-only-identifier, let O be the current self and let M be the method-only-identifier. Create an empty list of arguments L.
    • If the method-identifier of the primary-method-invocation occurs:
      1. Let O be the current self and let M be the method-identifier.
      2. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any.

        If the argument-with-parentheses does not occur, create an empty list of arguments L.

      3. If the block occurs, let B be the block.
    • If the . of the primary-method-invocation occurs:
      1. Evaluate the primary-expression and let O be the resulting value. Let M be the method-name.
      2. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any.

        If the argument-with-parentheses does not occur, create an empty list of arguments L.

      3. If the block occurs, let B be the block.
    • If the :: and method-name of the primary-method-invocation occur:
      1. Evaluate the primary-expression and let O be the resulting value. Let M be the method-name.
      2. Construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1. Let L be the resulting list. Let B be the resulting block, if any.
      3. If the block occurs, let B be the block.
    • If the :: and method-name-without-constant of the primary-method-invocation occur:
      1. Evaluate the primary-expression and let O be the resulting value. Let M be the method-name-without-constant.
      2. Create an empty list of arguments L.
      3. If the block occurs, let B be the block.
  2. Invoke the method M on O with L as the list of arguments and B, if any, as the block. (see §13.3.3). The resulting value is the value of the primary-method-invocation.

An indexing-method-invocation is evaluated as follows:

  1. Evaluate the primary-expression. Let O be the resulting value.
  2. If the indexing-argument-list occurs, construct a list of arguments from the indexing-argument-list as described in §11.2.1. Let L be the resulting list.
  3. If the indexing-argument-list does not occur, Create an empty list of arguments L.
  4. Invoke the method [] on O with L as the list of arguments. The resulting value is the value of the indexing-method-invocation.

A method-invocation-without-parentheses is evaluated as follows:

  • If the method-invocation-without-parentheses is a command, evaluate it. The resulting value is the value of the method-invocation-without-parentheses.
  • If the method-invocation-without-parentheses is a return-with-argument, break-with-argument or next-with-argument, evaluate it (see §11.4.1.3).
  • If the chained-command-with-do-block of the method-invocation-without-parentheses occurs:
    1. Evaluate the chained-command-with-do-block. Let V be the resulting value.
    2. If the method-name and the argument of the method-invocation-without-parentheses occur:
      1. Let M be the method-name.
      2. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list. If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds.
      3. Invoke the method M on V with L as the list of arguments and B, if any, as the block.
      4. Replace V with the resulting value.
    3. The value of the method-invocation-without-parentheses is V.

A command is evaluated as follows:

  1. If the command is a super-with-argument or a yield-with-argument, evaluate it.
  2. Otherwise:
    1. If the method-identifier of the command occurs:
      1. Let O be the current self and let M be the method-identifier.
      2. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list.

        If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds.

    2. If the primary-expression, method-name, and the argument of the command occurs:
      1. Evaluate the primary-expression. Let O be the resulting value. Let M be the method-name.
      2. Construct a list of arguments from the argument as described in §11.2.1 and let L be the resulting list.

        If the block-argument of the argument-in-parentheses of the argument occurs, let B be the block to which the block-argument corresponds.

    3. Invoke the method M on O with L as the list of arguments and B, if any, as the block. The resulting value is the value of the command.


A chained-command-with-do-block is evaluated as follows:

  1. Evaluate the command-with-do-block and let V be the resulting value.
  2. For each chained-method-invocation, in the order they appears in the program text, take the following steps:
    1. Let M be the method-name of the chained-method-invocation.
    2. If the argument-with-parentheses occurs, construct a list of arguments and a block from the argument-with-parentheses as described in §11.2.1 and let L be the resulting list. Let B be the resulting block, if any.

      If the argument-with-parentheses does not occur, create an empty list of arguments L.

    3. Invoke the method M on V with L as the list of arguments and B, if any, as the block.
    4. Replace V with the resulting value.
  3. The value of the chained-command-with-do-block is V.

A command-with-do-block is evaluated as follows:

  • If the command-with-do-block is a super-with-argument-and-do-block, evaluate it. The resulting value is the value of the command-with-do-block.
  • Otherwise:
    1. If the method-identifier of the command occurs, let O be the current self and let M be the method-name.

      If the method-identifier of the command does not occur, evaluate the primary-expression, let O be the resulting value and let M be the method-name.

    2. Construct a list of arguments from the arguments of the command-with-do-block and let L be the resulting list.
    3. Invoke the method M on O with L as the list of arguments and the do-block as the block. The resulting value is the value of the command-with-do-block.

11.2.1 Method arguments

11.2.1: Method arguments

Syntax

 indexing-argument-list ::
     command
   | operator-expression-list ,?
   | operator-expression-list , splatting-argument
   | association-list ,?
   | splatting-argument
 
 splatting-argument ::
     * operator-expression
   
 operator-expression-list ::
     operator-expression ( , operator-expression )*
   
 argument-with-parentheses ::
     ()
   | ( argument-in-parentheses )
   | ( operator-expression-list , chained-command-with-do-block )
   | ( chained-command-with-do-block )
 
 argument ::
     [ no line-terminator here ] [ lookahead \notin { { } ] optional-whitespace? argument-in-parentheses
   
 argument-in-parentheses ::
     command
   | ( operator-expression-list | association-list ) ( , splatting-argument )? ( , block-argument )?
   | operator-expression-list , association-list ( , splatting-argument )? ( , block-argument )?
   | splatting-argument ( , block-argument )?
   | block-argument
 
 block-argument ::
     & operator-expression

The operator-expression of a splatting-argument, operator-expression-list, and block-argument shall not be a jump-expression.

If the operator-expression-list of an argument-in-parentheses occurs, the first operator-expression of the operator-expression-list is called the first argument.

If a splatting-argument is the first argument, whitespaces shall not occur between its * and operator-expression. If a block-argument is the first argument, whitespaces shall not occur between its & and operator-expression.

If the first argument of an argument is other than the following constructs, the optional-whitespace shall occur.

  • A variable-reference of the form global-variable-identifier, class-variable-identifier or instance-variable-identifier (see §11.4.3).
  • A single-quoted-string or double-quoted-string (see §8.5.5.2).
  • A symbol-literal, or a dynamic-symbol of the form : [no whitespace here] single-quoted-string or : [no whitespace here] double-quoted-string (see §8.5.5.5).
  • An external-command-execution of the form backquoted-external-command-execution (see §8.5.5.2.6).
  • A scoped-constant-reference whose primary-expression occurs and the primary-expression is any of these constructs.
  • A primary-method-invocation whose primary-expression occurs and the primary-expression is any of these constructs.

Semantics

The list of arguments used for method invocation is constructed from indexing-argument-list, splatting-argument, argument-with-parentheses, or argument.

An indexing-argument-list is processed as follows:

  1. Create an empty list of arguments L.
  2. Evaluate the command, operator-expressions of operator-expression-lists, and the association-list and append their values to L in the order they appear in the program text.
  3. If the splatting-argument occurs, construct a list of arguments from it and concatenate the resulting list to L.

A splatting-argument is processed as follows:

  1. Create an empty list of arguments L.
  2. Evaluate the operator-expression. Let V be the resulting value.
  3. If V is not an instance of the class Array, the behavior is implementation dependent.
  4. Append each element of V, in the indexing order, to L.

An argument-with-parentheses is processed as follows:

  1. Create an empty list of arguments L.
  2. If the argument-in-parentheses occurs, construct a list of arguments from it and concatenate the resulting list to L. If block-argument of argument-in-parentheses occurs, the block to which the block-argument corresponds is the block which is passed to the method invocation with L.
  3. If the operator-expression-list occurs, for each operator-expression of the operator-expression-list, in the order they appears in the program text, take the following steps:
    1. Evaluate the operator-expression. Let V be the resulting value.
    2. Append V to L.
  4. If the chained-command-with-do-block occurs, evaluate it. Append the resulting value to L.

An argument is processed as follows:

  1. Evaluate the argument-in-parentheses.
  2. Let L be the resulting list.

An argument-in-parentheses is processed as follows:

  1. Create an empty list of arguments L.
  2. If the command occurs, evaluate it. Append the resulting value to L.
  3. If the operator-expression-list occurs, for each operator-expression of the operator-expression-list, in the order they appears in the program text, take the following steps:
    1. Evaluate the operator-expression. Let V be the resulting value.
    2. Append V to L.
  4. If the association-list occurs, evaluate it. Append the resulting value to L.
  5. If the splatting-argument occurs, construct a list of arguments from it and concatenate the resulting list to L.
  6. If the block-argument occurs, construct a block which is passed to a method invocation as described below.

A block which is passed to a method invocation is constructed from the block-argument as follows:

  1. Evaluate the operator-expression. Let P be the resulting value.
  2. If P is not an instance of the class Proc, the behavior is implementation dependent.
  3. Otherwise, the resulting block is the block which P represents.

11.2.2 Blocks

11.2.3 The super expression

11.2.4 The yield expression

11.3 Operator expressions

11.3.1 Assignments

11.3.1.1 Single assignments
11.3.1.1.1 Single variable assignments
11.3.1.1.2 Single indexing assignments
11.3.1.1.3 Single method assignments
11.3.1.3 Multiple assignments
11.3.1.4 Assignments with rescue modifiers

11.3.2 Unary operators

11.3.2.1 The defined? expression

11.3.3 Binary operators

11.4 Primary expressions

11.4.1 Control structures

11.4.1.1 Conditional expressions
11.4.1.1.1 The if expression
11.4.1.1.2 The unless expression
11.4.1.1.3 The case expression
11.4.1.1.4 Conditional operator
11.4.1.2 Iteration expressions
11.4.1.2.1 The while expression
11.4.1.2.2 The until expression
11.4.1.2.3 The for expression
11.4.1.3 Jump expressions
11.4.1.3.1 The return expression
11.4.1.3.2 The break expression
11.4.1.3.3 The next expression
11.4.1.3.4 The redo expression
11.4.1.3.5 The retry expression
11.4.1.4 Exceptions
11.4.1.4.1 The rescue expression

11.4.2 Grouping expressions

11.4.3 Variable references

11.4.3.1 Constants
11.4.3.2 Scoped constants
11.4.3.3 Global variables
11.4.3.4 Class variables
11.4.3.5 Instance variables
11.4.3.6 Local variables
11.4.3.7 Pseudo variables
11.4.3.7.1 nil
11.4.3.7.2 true and false
11.4.3.7.3 self

11.4.4 Object constructors

11.4.4.1 Array constructor
11.4.4.2 Hash constructor
11.4.4.3 Range constructor

11.4.5 Literals

Personal tools