Skip to main content

Operator Semantics

The following sections list all the operators and their semantic depending on the types of the operands. The left column represents the left-hand side operand type while the top row represents the right-hand side operand type. The values describe the semantic of the operation or list error if the corresponding operator cannot be applied with the given operand types.

Arithmetic Operators

+

+NumberStringArrayHashRes CollectionOther
NumberAdditionErrorErrorErrorErrorError
StringConcatenationConcatenationErrorErrorErrorError
Date/TimeAddition (seconds)ErrorErrorErrorErrorError
ArrayErrorErrorConcatenationErrorErrorError
HashErrorErrorErrormerge left into rightErrorError
Res CollectionErrorErrorErrorErrorConcatenationError
OtherErrorErrorErrorErrorErrorError

-

-NumberDate/TimeArrayRes CollectionOther
NumberSubtractionErrorErrorErrorError
Date/TimeSubtraction (seconds)Subtraction (seconds)ErrorErrorError
ArrayErrorErrorArray DifferenceErrorError
Res CollectionErrorErrorErrorColl DifferenceError
OtherErrorErrorErrorErrorError

*

*NumberOther
NumberMultiplicationError
OtherErrorError

/

/NumberOther
NumberDivision (integral, if both numbers are integers)Error
OtherErrorError

%

%NumberOther
NumberModulo (non integer numbers are rounded)Error
OtherErrorError

^

-NumberOther
NumberPowerError
OtherErrorError

Collection and Array Operators

<<

<<All
ArrayAppend object in right-hand side to array in left-hand side
OtherError

[ ]

The slice operator returns elements of an array, a hash or a resource collection using an index or a range (array and resource collection) or a key (hash). A range is specified using x..y where x and y are optional (no x means from the start of the array or collection, no y means to the end of the array or collection).

note

Retrieving elements of a resource collection creates a new resource collection consisting of these elements.

[ ]NumberStringRangeOther
Res CollectionCreates a new collection consisting of the element at a given indexErrorCreates a new collection consisting of the element in the given range ( x\.\.y where x and y are optional)Error
ArrayValue at given indexErrorCreates a new array consisting of the element in the given range ( x\.\.y where x and y are optional)Error
HashErrorValue associated with given key if any, null otherwiseErrorError
StringCreates a string composed of a character at a given indexErrorCreate a string made of characters in the given range. Range bounds may have negative values (from last or from beginning).Error
OtherErrorErrorErrorError

[ ]=

The slice assign operator replaces an element of an array, hash or resource collection or a range of an array or resource collection with given elements.

[ ] =NumberStringRangeOther
Res CollectionReplaces the resource at a given index. Right-hand side must be a resource collection.ErrorReplace elements in the given range with elements of the right-hand side resource collection.Error
ArrayReplace the value at a given index.ErrorReplace the elements in the given range with elements of the given array. The right-hand side must be an array.Error
HashErrorReplace/set the value associated with a given key.ErrorError
OtherErrorErrorErrorError

Regular Expression-Include Operators

=~

=~StringArrayOther
Stringtrue if regular expression given in right-operand matches string given in left operandErrorError
Arraytrue if array contains stringtrue if array contains arraytrue if array contains value
Hashtrue if hash has that keyErrorError
OtherErrorErrorError

The right-hand side of a regular expression comparison may use the symbol / to delineate the pattern. The ending / may be followed by the following options:

  • i for case insensitive matching
  • m for multi-line string matching
  • x for extended syntax support (whitespaces in the pattern are ignored)

Examples:

"foo" =~ "foo" # true
"foo" =~ /foo/ # true
"foo" =~ /FOO/i # true

!~

a !~ b is equivalent to !(a =~ b).

Relational and Equality Operators

==, !=

Equality comparison is done recursively if values are arrays or hashes.

==All
Alltrue if operands have same value, false otherwise

<, <=, >, >=

<NumberStringDate/TimeRes CollectionArrayOther
NumberLess thanErrorErrorErrorErrorError
StringErrorLexicographical less thanErrorErrorErrorError
Date/TimeErrorErrorLess thanErrorErrorError
Res CollectionErrorErrorError@a &lt; @b : true if all elements of @a are also elements of @b .ErrorError
ArrayErrorErrorErrorError@a &lt; @b : true if all elements of @a are also elements of @b .Error
OtherError
note

>, <=, and >= are omitted for brevity (greater than, lesser or equal than, greater or equal than, respectively).

Logical Operators

<

"Bitwise and". Applied to resource collections (or arrays) it returns all elements that appear in both resource collections (or arrays).

<falsenulltrueArrayRes CollectionOther
falsefalsefalsefalsefalsefalsefalse
nullfalsefalsefalsefalsefalsefalse
truefalsefalsetruetruetruetrue
Arrayerrorerrorerrorintersection of two arrayserrorerror
Res Collectionerrorerrorerrorerrornew collection with common hrefs or error if collections are of different typeserror
Othererrorerrorerrorerrorerrorerror

<<

"Logical and". Returns the right-hand side operand if the left-hand side is neither false nor null (otherwise returns left-hand side operand). In the following table, (1) and (2) refer to the position of that particular argument in the expression.

<<falsenulltrueOther (1)
falsefalsefalsefalsefalse
nullnullnullnullnull
truefalsenulltrue(1)
Other (2)falsenulltrue(2)

|

"Bitwise or". Applied to resource collections (or arrays), returns all elements that appear in either resource collection (or array).

note

The "|" operator differs from the "+" operator when applied to a collection. "|" will produce only one instance of each element in each collection even if the same element appears in both.

|falsenulltrueOther
falsefalsefalsetruetrue
nullfalsefalsetruetrue
truetruetruetruetrue
Othererrorerrorerrorerror

||

"Logical or". Returns the left-hand side operand if it is neither false nor null (otherwise returns right-hand side operand). In the following table, (1) and (2) refer to the position of that particular argument in the expression.

||falsenulltrueOther (2)
falsefalsenulltrue(2)
nullfalsenulltrue(2)
truetruetruetruetrue
Other (1)(1)(1)(1)(1)

Unary Operators

-

"-" is the unary minus operator.

-Value
NumberOpposite value
OtherError

\

! is the logical not operator.

note

"!" applies to all types (negating a non-boolean value returns true if it is either false or null, false otherwise).

!Value
falsetrue
nulltrue
Otherfalse