|
Most of Ruby's operators are method calls.This also means they can be redefined.We will discuss what methods are in a future chapter.
In order of precedence, a table with all operators, method calls and non-method calls:
The non-method call ones:
Most commonly used operatos:
arithmetic operators such as "+" or "-"
logical operators such as "&&" equivalent to "and", the "||" operator equivalent to "or", the "!=" equivalent to "not equal to", or the "<>" equivalent to "different than"
the ternary operator "?" used as a short form of an if statement; use it to test the value of truth of an expression and execute code depending if true or false; (expr) ? (excuteiftrue):(executeiffalse)
Examples of use:
x=1 x is attributed the value of 1
x+=2 x is attributed the value of x + 2
x<>y x different than y
x>=y x larger or equal than y
(x>y) ? (x+=1):(x-=1) test if x larger than y, if true add 1 to x, if false subtract 1 from x
|