Operator Expressions

Example:

1+2*3/4

Some method calls and control structures take the form of operators for ease in programming. Ruby contains the following operators:

high   ::
       []
       **
       -(unary)  +(unary)  !  ~
       *  /  %
       +  -
       << >>
       &
       |  ^
       > >=  < <=
       <=> ==  === !=  =~  !~
       &&
       ||
       ..  ...
       ?:(conditional operator)
       =(+=, -= ... )
       not
low    and or

"High" and "low" signify the operators' priority level. For example, "&&" has a higher priority than "||", so it would be interpreted in the following way:

a && b || c   # => (a && b) || c
a || b && c   # =>  a || (b && c)

Most operators are method calls in special form, but some are built into the language and cannot be redefined.

Assignment

Example:

foo = bar
foo[0] = bar
foo.bar = baz

Syntax:

variable '=' expr
constant '=' expr
expr '['expr..']' '=' expr
expr '.' identifier '=' expr

Assignment expressions are used to assign values to variables and the like. Assignments can also be used as declarations for local variables or constants. The left side of an assignment expression must be one of the following:

Self-Assignment

Example:

foo += 12       # foo = foo + 12
foo *= 3        # foo = foo * 3

Syntax:

expr1 op= expr2     # expr1 is part of the right-hand assignment

op is one of the following. There can be no space between the operator and =.

+, -, *, /, %, **, &, |, ^, <<, >>, &&, ||

In this assignment format, most cases are evaluated as:

expr1 = expr1 op expr2

Multiple Assignment

Example:

foo, bar, baz = 1, 2, 3
foo, = list()
foo, *rest = list2()

Syntax:

expr [',' [ expr ',' ... ] ['*' [ expr ]]] = expr [, expr ... ]['*' expr ]
'*' [ expr ] = expr [, expr ... ]['*' expr ]

Multiple assignment performs assignments from multiple expressions or arrays. Each expression on the left must be assignable. If there is only one expression on the right, its value will be converted into an array whose elements will be assigned to the expressions on the left. If there are more elements in the array than on the left, the extra elements are ignored. If there are too few elements in the array, nil will be assigned to the extra elements on the left.

Prepend * to the final expression on the left to assign all extra lefthand elements to that expression as an array. If there are no extra elements, an empty array will be assigned.

foo, bar  = [1, 2]      # foo = 1; bar = 2
foo, bar  = 1, 2        # foo = 1; bar = 2
foo, bar  = 1           # foo = 1; bar = nil
foo, bar  = 1, 2, 3     # foo = 1; bar = 2
foo       = 1, 2, 3     # foo = [1, 2, 3]
*foo      = 1, 2, 3     # foo = [1, 2, 3]
foo, *bar = 1, 2, 3     # foo = 1; bar = [2, 3]

and

Example:

test && set
test and set

Syntax:

expr '&&' expr
expr and expr

First evaluates the left side; if the result is true, evaluates the right side. and does the same as &&, but is a lower priority operator.

or

Example:

demo || die
demo or die

Syntax:

expr '||' expr
expr or expr

First evaluates the left side; if the result is false, evaluates the right side. or does the same as ||, but is a lower priority operator.

not

Example:

! me
not me
i != you

Syntax:

'!' expr
not expr

If the value of the expression is true, returns FALSE; if the value is false, returns TRUE.

The following notation is also possible:

expr '!=' expr          # same as !(expr == expr)
expr '=~' expr          # same as !(expr =~ expr)

Conditional Operators

Example:

obj == 1 ? foo : bar

Syntax:

expr1 ? expr2 : expr3

Returns expr2 or expr3 depending on the results of expr1. This is identical to:

if expr1 then expr2 else expr3 end
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)