Operators

In Ruby nearly all operators are class methods: types, operators and functions are all joined in an strong object oriented paradigm.

Only the assignment operators and some logical operators are not implemented as class methods. These operators are:

Operator Meaning
= assignment : a=1
+= -= *= operation and re-assignment, implemented for all numerical operators
:? ternary operator
&& logical and
|| logical or
not logical negation
or logical operator "or" with lower precedence
and logical operator "and"with lower precedence
.. ... range operators (create range objects)
defined? nil if not defined, otherwise the type: Es.: a=1; defined?(a) => "local-variable"

The assignment operator "=" defines a reference to an object, and, for some basic objects, creates an instance of the object:

anum=1       #  creates an instance of the Integer class (the number one)
             #  and a reference (the name "anum") to the instance


bnum=anum    # creates a second reference to the same integer

In the following example, when a new instance of Integer (the number six) is created, the anum reference is redefined, but bnum continues to reference to the number one:

anum=1
bnum=anum
anum=6

If instead of numbers we had a composite object, like an array, we could change the object using any of the references (anum or bnum) and both see the modified object; this can confuse inexperienced users:

anum=["a","b","c"]   # anum is a reference to an object array.
bnum=anum            # I make a copy of the reference,

anum[0]="k"          # Now I change the first element,
;                    # and both bnum and anum point to: ["k", "b", "c"]

Multiple assignments are possible:

a=b=c=3       # a,b,c , all reference to '3'
a,b=1,2       # a refers to one, b to two
a,b=1,2,3,4   # extra elements are ignored
a,b,c=1,2     # c is unassigned (c==nil)

Many values can be assigned to a single array, or an array can be expanded to single values in an assignment; in this case the "*" operator is used: it is called "splat" operator:

a,b=[1,2]     # the assignment can be used to expand an array

x,*y=[1,2,3]  # extra elements are assigned to an array:  x => 1 ; y => [2, 3]

*y,x=[2,3,1]       # since Ruby 1.9 the array can be in the first position
a,*y,b=1,2,3,4,5   # and can also be in the middle: a => 1 ; y => [2, 3, 4] ; b => 5

x,y,z=1,*[2,3]     # assignment can be used to expand an array: x=>1 ; y => 2 ; z => 3

The operators of the form: "+=,-=,*=" etc. are shorthands for operation and assignment.

The "++" operator doesn't exists in Ruby.

a=a+1    # same as: a+=1
a=a-1    # same as: a-=1

The logical operators: "and" ,"or" ,"&&" , "||" , evaluate the second operand only if the first doesn't define the relation:

a && b   #  evaluates 'a' ;
         #  if 'a' is false (or nil), returns 'a' (false),
         #  otherwise evaluates and returns 'b'


a || b   # evaluates 'a' ;
         # if true returns 'a',
         # otherwise evaluates and returns 'b'

This can be used in conditional expressions to execute a routine, as in the Bash shell, or to define a variable only if not yet defined:

condition && func("aaa")  # 'func' is executed only if the condition is true

var ||  =77               # an undefined variable is false; the assignment is executed.

The ternary operators evaluates a condition, if true returns the first expression, otherwise the second:

1==1 ? 'Yes' : 'No'   # the condition is true, the operator returns: 'yes'

1==2 ? 'yes' : 'No'   # the condition is false, the operator returns: 'No'