# Ruby Programming/Syntax/Operators
## Operators
### Complete List and Precedence

| Operator | Name/meaning | Arguments | Precedence | Association |
| ! | Boolean NOT | Unary | 1 | Right |
| ~ | Bitwise complement | Unary | 1 | Right |
| + | Unary plus (no effect) | Unary | 1 | Right |
| ** | Exponentiation | Binary | 1 | Right |
| - | Unary minus (reverse sign) | Unary | 2 | Right |
| * | Multiplication | Binary | 3 | Left |
| / | Division | Binary | 3 | Left |
| % | Modulo (remainder) | Binary | 3 | Left |
| + | Addition or concatenation | Binary | 4 | Left |
| - | Subtraction | Binary | 4 | Left |
| << | Bitwise shift left or append (<< and <<- are also used in "here doc" notation) | Binary | 5 | Left |
| >> | Bitwise shift right | Binary | 5 | Left |
| & | Bitwise AND | Binary | 6 | Left |
| | | Bitwise OR | Binary | 7 | Left |
| ^ | Bitwise XOR | Binary | 7 | Left |
| < | Less-than | Binary | 8 | Left |
| <= | Less-than or equal-to | Binary | 8 | Left |
| > | Greater-than | Binary | 8 | Left |
| >= | Greater-than or equal to | Binary | 8 | Left |
| == | Equal (evaluate to the same value) | Binary | 9 | N/A |
| === | "Case equality", "case subsumption" or "three equals" operator. A === B if B is a member of the set of A. Operation varies considerably depending on the data types of A and B. | Binary | 9 | N/A |
| != | Not equal | Binary | 9 | N/A |
| =~ | Pattern match | Binary | 9 | N/A |
| !~ | Negative pattern match | Binary | 9 | N/A |
| <=> | A <=> B evaluates to -1, 0, or 1; if A is less-than, equal-to, or greater-than B, respectively | Binary | 9 | N/A |
| && | Boolean AND | Binary | 10 | Left |
| || | Boolean OR | Binary | 11 | Left |
| .. | Range creation, boolean flip-flop | Binary | 12 | N/A |
| ... | Open-ended range creation, boolean flip-flop | Binary | 12 | N/A |
| ?: | A?B:C evaluates to B if A is true, or C if A is false | Trinary | 13 | Right |
| rescue | Modifier for catching exceptions e.g. array[3] rescue nil | Binary | 14 | Left |
| = | Assignment | Binary | 15 | Right |
| **= | A **=B does A = A ** B | Binary | 15 | Right |
| *= | A *=B does A = A * B | Binary | 15 | Right |
| /= | A /=B does A = A / B | Binary | 15 | Right |
| %= | A %=B does A = A % B | Binary | 15 | Right |
| += | A +=B does A = A + B | Binary | 15 | Right |
| -= | A -=B does A = A – B | Binary | 15 | Right |
| <<= | A <<=B does A = A << B | Binary | 15 | Right |
| >>= | A >>=B does A = A >> B | Binary | 15 | Right |
| &&= | A &&=B assigns B to A if A is true or not nil | Binary | 15 | Right |
| &= | A &=B does A = A & B | Binary | 15 | Right |
| ||= | A ||=B assigns B to A if A is nil or false | Binary | 15 | Right |
| |= | A |= B does A = A | B | Binary | 15 | Right |
| ^= | A ^=B does A = A ^ B | Binary | 15 | Right |
| defined? | nil if the expression cannot be evaluated (e.g. unset variable) | Unary | 16 | N/A |
| not | Boolean NOT | Unary | 17 | Right |
| and | Boolean AND | Binary | 18 | Left |
| or | Boolean OR | Binary | 18 | Left |
| if | Conditional, e.g. print x if x | Binary | 19 | N/A |
| unless | Negative conditional, e.g. x = 0 unless x | Binary | 19 | N/A |
| while | Loop conditional, e.g. print x += 1 while (x < 10) | Binary | 19 | N/A |
| until | Loop conditional, e.g. print x += 1 until (x == 10) | Binary | 19 | N/A |
Higher precedence (lower number in the above table) operators have their immediate arguments evaluated first. Precedence order can be altered with blocks. For example, because * has higher precedence than +, then:
1 + 2 * 3 == 7 (1 + 2) * 3 == 9
Association direction controls which operators have their arguments evaluated first when multiple operators with the same precedence appear in a row. For example, because - has left association:
1 – 2 – 3 == (1 – 2) – 3 == -1 – 3 == -4
instead of:
1 – 2 – 3 == 1 – (2 – 3) == 1 - -1 == 2
Because ** has right association:
2 ** 3 ** 2 == 2 ** (3 ** 2) == 2 ** 9 == 512
instead of:
2 ** 3 ** 2 == (2 ** 3) ** 2 == 8 ** 2 == 64
 blocks have lower precedence than the above operators, followed by do/end blocks. Array accesses with can be thought of as having a higher precedence than any above operator.
The operators ** through !~ can be overridden (defined for new classes, or redefined for existing operations).
Note that rescue, if, unless, while, and until are operators when used as modifiers in one-liners (as in the above examples) but can also be used as keywords.

### Other operators

The dot operator . is used for calling methods on objects, also known as passing a message to the object.
Ruby 2.3.0 introduced the safe navigation operator &., also known as the "lonely operator". This allows replacing
with
An equivalent .dig method was introduced for hashes and arrays:
are safer versions of:
The safe navigation operator will raise an error if a requested method, key, or index is not available; unlike the technique of using try for this purpose, which will return nil.
Yukihiro Matsumoto considered ! ?. and .? before settling on &. because:
* ?. conflicts with *?
* ?. is used by other languages, thus .? is confusingly similar but different
* ! conflicts with "not" logic
* ? is already used by convention for functions that return booleans
* &. is reminiscent of the && syntax the operator is replacing
!! is sometimes seen, but this is simply the ! operator twice. It is used to force the following expression to evaluate to a boolean. This technique is considered non-idiomatic and poor programming practice, because there are more explicit ways to force such a conversion (which is rarely needed to begin with).

### Assignment

Assignment in Ruby is done using the equal operator "=". This is both for variables and objects, but since strings, floats, and integers are actually objects in Ruby, you're always assigning objects.
Examples:
Self assignment
A frequent question from C and C++ types is "How do you increment a variable? Where are ++ and -- operators?" In Ruby, one should use x+=1 and x-=1 to increment or decrement a variable.
Multiple assignments
Examples:
Conditional assignment
Operator ||= is a shorthand form that closely resembles the expression:
Operator ||= can be shorthand for code like:
In same way &&= operator works:
Operator &&= is a shorthand form of the expression:

## Scope

In Ruby there's a local scope, a global scope, an instance scope, and a class scope.

### Local Scope

Example:
This error appears because this x(toplevel) is not the x(local) inside the do..end block the x(local) is a local variable to the block, whereas when trying the puts x(toplevel) we're calling a x variable that is in the top level scope, and since there's not one, Ruby protests.

### Global scope

This output is given because prefixing a variable with a dollar sign makes the variable a global.

### Instance scope

Within methods of a class, you can share variables by prefixing them with an @.

### Class scope

A class variable is one that is like a "static" variable in Java. It is shared by all instances of a class.
Here's a demo showing the various types:
This will print the two lines "kiwi" and "kiwi told you so!!", then fail with a undefined local variable or method 'localvar' for #<Test:0x2b36208 @instvar="kiwi"> (NameError). Why? Well, in the scope of the method print_localvar there doesn't exists localvar, it exists in method initialize(until GC kicks it out). On the other hand, class variables '@@classvar' and '@instvar' are in scope across the entire class and, in the case of @@class variables, across the children classes.
Class variables have the scope of parent class AND children, these variables can live across classes, and can be affected by the children actions ;-)
This new child of Test also has @@classvar with the original value newvar.print_classvar. The value of @@classvar has been changed to 'kiwi kiwi waaai!!' This shows that @@classvar is "shared" across parent and child classes.

## Default scope

When you don't enclose your code in any scope specifier, ex:
it affects the default scope, which is an object called "main".
For example, if you had one script that says
and other_script.rb says
They could share variables.
Note however, that the two scripts don't share local variables.

## Local scope gotchas

Typically when you are within a class, you can do as you'd like for definitions, like.
And also, procs "bind" to their surrounding scope, like
However, the "class" and "def" keywords cause an *entirely new* scope.
You can get around this limitation by using define_method, which takes a block and thus keeps the outer scope (note that you can use any block you want, to, too, but here's an example).
Here's using an arbitrary block
or here

## Logical And

The binary "and" operator will return the logical conjunction of its two operands. It is the same as "&&" but with a lower precedence. Example:

## Logical Or

The binary "or" operator will return the logical disjunction of its two operands. It is the same as "||" but with a lower precedence. Example:

## References

* ↑ http://ruby-doc.org/core-2.4.0/doc/syntax/precedence_rdoc.html
* ↑ https://www.ruby-lang.org/en/news/2015/12/25/ruby-2-3-0-released/
* ↑ http://blog.rubyeffect.com/ruby-2-3s-lonely-operator/
* ↑ https://bugs.ruby-lang.org/issues/11537
* ↑ http://www.rubyinside.com/what-rubys-double-pipe-or-equals-really-does-5488.html