Ruby - Operators

Ruby supports a rich set of operators, as you'd expect from a modern language. Most operators are actually method calls. 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.
For each operator (+ - * / % ** & | ^ << >> && ||), there is a corresponding form of abbreviated assignment operator (+= -= etc.).
Assume variable a holds 10 and variable b holds 20, then −
Assume variable a holds 10 and variable b holds 20, then −
Assume variable a holds 10 and variable b holds 20, then −
Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code. For example −
a = 10
b = 20
c = 30

This may be more quickly declared using parallel assignment −
a, b, c = 10, 20, 30

Parallel assignment is also useful for swapping the values held in two variables −
a, b = b, c

Bitwise operator works on bits and performs bit by bit operation.
Assume if a = 60; and b = 13; now in binary format they will be as follows −
 a    =  0011 1100
 b    =  0000 1101
 ------------------
 a&b  =  0000 1100
 a|b  =  0011 1101
 a^b  =  0011 0001
 ~a   =  1100 0011

The following Bitwise operators are supported by Ruby language.
The following logical operators are supported by Ruby language
Assume variable a holds 10 and variable b holds 20, then −
There is one more operator called Ternary 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. The conditional operator has this syntax −
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.
In Ruby, these sequences are created using the ".." and "..." range operators. The two-dot form creates an inclusive range, while the three-dot form creates a range that excludes the specified high value.
defined? is a special operator that takes the form of a method call to determine whether or not the passed expression is defined. It returns a description string of the expression, or nil if the expression isn't defined.
There are various usage of defined? Operator
defined? variable # True if variable is initialized
For Example
foo = 42
defined? foo    # => "local-variable"
defined? $_     # => "global-variable"
defined? bar    # => nil (undefined)
defined? method_call # True if a method is defined
For Example
defined? puts        # => "method"
defined? puts(bar)   # => nil (bar is not defined here)
defined? unpack      # => nil (not defined here)
# True if a method exists that can be called with super user
defined? super
For Example
defined? super     # => "super" (if it can be called)
defined? super     # => nil (if it cannot be)
defined? yield   # True if a code block has been passed
For Example
defined? yield    # => "yield" (if there is a block passed)
defined? yield    # => nil (if there is no block)
You call a module method by preceding its name with the module's name and a period, and you reference a constant using the module name and two colons.
The :: is a unary operator that allows: constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or module.
Remember in Ruby, classes and methods may be considered constants too.
You need to just prefix the :: Const_name with an expression that returns the appropriate class or module object.
If no prefix expression is used, the main Object class is used by default.
Here are two examples −
MR_COUNT = 0         # constant defined on main Object class
module Foo
   MR_COUNT = 0
   ::MR_COUNT = 1    # set global count to 1
   MR_COUNT = 2      # set local count to 2
end
puts MR_COUNT        # this is the global constant
puts Foo::MR_COUNT   # this is the local "Foo" constant
Second Example
CONST = ' out there'
class Inside_one
   CONST = proc {' in there'}
   def where_is_my_CONST
      ::CONST + ' inside one'
   end
end
class Inside_two
   CONST = ' inside two'
   def where_is_my_CONST
      CONST
   end
end
puts Inside_one.new.where_is_my_CONST
puts Inside_two.new.where_is_my_CONST
puts Object::CONST + Inside_two::CONST
puts Inside_two::CONST + CONST
puts Inside_one::CONST
puts Inside_one::CONST.call + Inside_two::CONST
The following table lists all operators from highest precedence to lowest.
NOTE − Operators with a Yes in the method column are actually methods, and as such may be overridden.

    46 Lectures 
     9.5 hours 

 Eduonix Learning Solutions

    97 Lectures 
     7.5 hours 

 Skillbakerystudios

    227 Lectures 
     40 hours 

 YouAccel

    19 Lectures 
     10 hours 

 Programming Line

    51 Lectures 
     5 hours 

 Stone River ELearning

    39 Lectures 
     4.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page