In Ruby, these sequences are created using the "." and "." range operators. Sequence ranges in Ruby are used to create a range of successive values - consisting of a start value, an end value, and a range of values in between. If Condition is true ? Then value X : Otherwise value Y
The conditional operator has this syntax − Operator It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation. There is one more operator called Ternary Operator. If a condition is true, then Logical NOT operator will make false. Use to reverses the logical state of its operand. If both the operands are non zero, then the condition becomes true.Ĭalled Logical NOT Operator. If any of the two operands are non zero, then the condition becomes true.Ĭalled Logical AND operator. If both the operands are true, then the condition becomes true.Ĭalled Logical OR Operator. The following logical operators are supported by Ruby languageĬalled Logical AND operator. The left operands value is moved right by the number of bits specified by the right operand. (~a ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number.īinary Right Shift Operator.
Operatorīinary AND Operator copies a bit to the result if it exists in both operands.īinary OR Operator copies a bit if it exists in either operand.īinary XOR Operator copies the bit if it is set in one operand but not both.īinary Ones Complement Operator is unary and has the effect of 'flipping' bits. The following Bitwise operators are supported by Ruby language. Parallel assignment is also useful for swapping the values held in two variables −īitwise operator works on bits and performs bit by bit operation.Īssume if a = 60 and b = 13 now in binary format they will be as follows − This may be more quickly declared using parallel assignment − This enables multiple variables to be initialized with a single line of Ruby code. Ruby also supports the parallel assignment of variables. Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand.Įxponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand.ĭivide AND assignment operator, divides left operand with the right operand and assign the result to left operand. Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand. Simple assignment operator, assigns values from right side operands to left side operand.Ĭ = a + b will assign the value of a + b into cĪdd AND assignment operator, adds right operand to the left operand and assign the result to left operand. If aObj is duplicate of bObj then aObj = bObj is true, a.equal?bObj is false but a.equal?aObj is true. True if the receiver and argument have the same object id.
True if the receiver and argument have both the same type and equal values.ġ = 1.0 returns true, but 1.eql?(1.0) is false. Used to test equality within a when clause of a case statement. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. Modulus − Divides left hand operand by right hand operand and returns remainder.Įxponent − Performs exponential (power) calculation on operators.Ĭhecks if the value of two operands are equal or not, if yes then condition becomes true.Ĭhecks if the value of two operands are equal or not, if values are not equal then condition becomes true.Ĭhecks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.Ĭhecks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.Ĭombined comparison operator. Multiplication − Multiplies values on either side of the operator.ĭivision − Divides left hand operand by right hand operand. Subtraction − Subtracts right hand operand from left hand operand. Ruby Arithmetic OperatorsĪssume variable a holds 10 and variable b holds 20, then − OperatorĪddition − Adds values on either side of the operator. For example, a + b is interpreted as a.+(b), where the + method in the object referred to by variable a is called with b as its argument.įor each operator (+ - * / % ** & | ^ > & ||), there is a corresponding form of abbreviated assignment operator (+= -= etc.). Most operators are actually method calls. Ruby supports a rich set of operators, as you'd expect from a modern language.