Expressions
From Ruby Standard Wiki
11 Expressions
Syntax
expression ::
keyword-logical-expression
Semantics
See §11.1 for keyword-logical-expression
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:
- If it is of the form
notkeyword-NOT-expression, evaluate the keyword-NOT-expression. Let X be the resulting value. - If it is a logical-NOT-expression, evaluate its method-invocation-without-parentheses or unary-expression. Let X be the resulting value.
- If X is a true value, the value of the keyword-NOT-expression or the logical-NOT-expression is
false. - 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:
- Evaluate the unary-expression or the method-invocation-without-parentheses. Let V be the resulting value.
- 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 classObjector one of its superclasses, if any. The method!@shall not take any arguments. The method!@shall returntrueif the receiver isfalseornil, and shall returnfalseotherwise.
A logical-AND-expression of the form logical-AND-expression && equality-expression or a keyword-AND-expression is evaluated as follows:
- Evaluate the expression or the logical-AND-expression. Let X be the resulting value.
- 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.
- 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:
- Evaluate the expression or the logical-OR-expression. Let X be the resulting value.
- 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.
- Otherwise, the value of the keyword-OR-expression or logical-OR-expression is X.
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
{ { } ] 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:
- Let M be the chained-command-with-do-block. One or more chained-method-invocation of M occurs.
- 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:
- 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:
- Let O be the current self and let M be the method-identifier.
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.
- If the block occurs, let B be the block.
- If the
.of the primary-method-invocation occurs:- Evaluate the primary-expression and let O be the resulting value. Let M be the method-name.
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.
- If the block occurs, let B be the block.
- If the
::and method-name of the primary-method-invocation occur:- Evaluate the primary-expression and let O be the resulting value. Let M be the method-name.
- 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 block occurs, let B be the block.
- If the
::and method-name-without-constant of the primary-method-invocation occur:- Evaluate the primary-expression and let O be the resulting value. Let M be the method-name-without-constant.
- Create an empty list of arguments L.
- If the block occurs, let B be the block.
- 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:
- Evaluate the primary-expression. Let O be the resulting value.
- 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.
- If the indexing-argument-list does not occur, Create an empty list of arguments L.
- 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:
- Evaluate the chained-command-with-do-block. Let V be the resulting value.
- If the method-name and the argument of the method-invocation-without-parentheses occur:
- Let M be the method-name.
- 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.
- Invoke the method M on V with L as the list of arguments and B, if any, as the block.
- Replace V with the resulting value.
- The value of the method-invocation-without-parentheses is V.
A command is evaluated as follows:
- If the command is a super-with-argument or a yield-with-argument, evaluate it.
- Otherwise:
- If the method-identifier of the command occurs:
- Let O be the current self and let M be the method-identifier.
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.
- If the primary-expression, method-name, and the argument of the command occurs:
- Evaluate the primary-expression. Let O be the resulting value. Let M be the method-name.
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.
- 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.
- If the method-identifier of the command occurs:
A chained-command-with-do-block is evaluated as follows:
- Evaluate the command-with-do-block and let V be the resulting value.
- For each chained-method-invocation, in the order they appears in the program text, take the following steps:
- Let M be the method-name of the chained-method-invocation.
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.
- Invoke the method M on V with L as the list of arguments and B, if any, as the block.
- Replace V with the resulting value.
- 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:
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.
- Construct a list of arguments from the arguments of the command-with-do-block and let L be the resulting list.
- 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
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
{ { } ] 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:
- Create an empty list of arguments L.
- 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.
- 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:
- Create an empty list of arguments L.
- Evaluate the operator-expression. Let V be the resulting value.
- If V is not an instance of the class
Array, the behavior is implementation dependent. - Append each element of V, in the indexing order, to L.
An argument-with-parentheses is processed as follows:
- Create an empty list of arguments L.
- 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.
-
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:
- Evaluate the operator-expression. Let V be the resulting value.
- Append V to L.
- If the chained-command-with-do-block occurs, evaluate it. Append the resulting value to L.
An argument is processed as follows:
- Evaluate the argument-in-parentheses.
- Let L be the resulting list.
An argument-in-parentheses is processed as follows:
- Create an empty list of arguments L.
- If the command occurs, evaluate it. Append the resulting value to L.
-
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:
- Evaluate the operator-expression. Let V be the resulting value.
- Append V to L.
- If the association-list occurs, evaluate it. Append the resulting value to L.
- If the splatting-argument occurs, construct a list of arguments from it and concatenate the resulting list to L.
- 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:
- Evaluate the operator-expression. Let P be the resulting value.
- If P is not an instance of the class
Proc, the behavior is implementation dependent. - Otherwise, the resulting block is the block which P represents.