Statements

From Ruby Standard Wiki

Jump to: navigation, search
12: Statements

Syntax

 statement ::
       expression-statement
     | alias-statement
     | undef-statement
     | if-modifier-statement
     | unless-modifier-statement
     | while-modifier-statement
     | until-modifier-statement
     | rescue-modifier-statement
     | rescue-modifier-statement
     | assignment-statement

See §13.3.6 for alias-statement.

See §13.3.7 for undef-statement.

See §11.3.1 for assignment-statement.

Semantics

See §11.3.1 for assignment-statement.

Contents

12.1 The expression statement

12.1: The expression statement

Syntax

 expression-statement ::
       expression

Semantics

An expression-statement is evaluated as follows:

  1. Evaluate the expression.
  2. The resulting value is the value of the expression-statement.

12.2 The if modifier statement

12.2: The if modifier statement

Syntax

 if-modifier-statement ::
       statement [no line-terminator here] if expression

The expression of an if-modifier-statement shall not be a jump-expression.

Semantics

An if-modifier-statement of the form S if E, where S is the statement and E is the expression, is evaluated as follows:

  1. Evaluate the if-expression of the form if E then S end.
  2. The resulting value is the value of the if-modifier-statement.

12.3 The unless modifier statement

12.3: The unless modifier statement

Syntax

 unless-modifier-statement ::
       statement [ no line-terminator here ] unless expression

The expression of an unless-modifier-statement shall not be a jump-expression.

Semantics

An unless-modifier-statement of the form S unless E, where S is the statement and E is the expression, is evaluated as follows:

  1. Evaluate the unless-expression of the form unless E then S end.
  2. The resulting value is the value of the unless-modifier-statement.

12.4 The while modifier statement

12.4: The while modifier statement

Syntax

 while-modifier-statement ::
       statement [ no line-terminator here ] while expression

The expression of an while-modifier-statement shall not be a jump-expression.

Semantics

An while-modifier-statement of the form S while E, where S is the statement and E is the expression, is evaluated as follows:

  1. Evaluate the while-expression of the form while E do S end.
  2. The resulting value is the value of the while-modifier-statement.

12.5 The until modifier statement

12.5: The until modifier statement

Syntax

 until-modifier-statement ::
       statement [ no line-terminator here ] until expression

The expression of an until-modifier-statement shall not be a jump-expression.

Semantics

An until-modifier-statement of the form S until E, where S is the statement and E is the expression, is evaluated as follows:

  1. Evaluate the until-expression of the form until E do S end.
  2. The resulting value is the value of the until-modifier-statement.

12.6 The rescue modifier statement

12.6: The rescue modifier statement

Syntax

 rescue-modifier-statement ::
       main-statement-of-rescue-modifier-statement [ no line-terminator here ] 
       rescue fallback-statement-of-rescue-modifier-statement
 main-statement-of-rescue-modifier-statement ::
       statement
 fallback-statement-of-rescue-modifier-statement ::
       statement but not statement-not-allowed-in-fallback-statement
 statements-not-allowed-in-fallback-statement ::
       keyword-AND-expression
     | keyword-OR-expression
     | if-modifier-statement
     | unless-modifier-statement
     | while-modifier-statement
     | until-modifier-statement
     | rescue-modifier-statement

Semantics

A rescue-modifier-statement is evaluated as follows:

  1. Evaluate the main-statement-of-rescue-modifier-statement. Let V be the resulting value.
  2. If an instance of the class StandardError is raised and not handled in Step a, evaluate fallback-statement-of-rescue-modifier-statement. The resulting value is the value of the rescue-modifier-statement.
  3. If no instances of the class Exception are raised in Step a, or all the instances of the class Exception raised in Step a are handled in Step a, the value of the rescue-modifier-statement is V.
Personal tools