Ruby supports a modern language of a rich array of operators. Most operators are actually method calls. For example, A + B is interpreted as a,
+ (b) The method of the object referenced by the variable is called a parameter with B as its.
For each operator (+-*/% * * & | ^ << >> && | |), there are corresponding assignment operator abbreviations (+ = = etc.)
Ruby arithmetic operators:
Assuming variable a=10, variable b=20:
Ruby comparison operator:
Assuming variable a=10, variable b=20:
Ruby Assignment Operator:
Assuming variable a=10, variable b=20:
Ruby Parallel Assignment:
Ruby also supports variables that are assigned in parallel. This allows multiple lines of ruby code to initialize the variable. For example:
Need to declare more quickly, using parallel assignment:
It is also useful to exchange values for two variables in parallel assignment:
Ruby Bitwise operators:
bitwise operator bit and bit operation execution bit.
Suppose when a = 60 and b=13; now in binary format 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
Bitwise operators that support the Ruby language
Ruby logical operators:
Logical operators that support Ruby languages
Assuming variable a=10, variable b=20:
Ruby ternary operator:
There is also an operator called a ternary operator. This first evaluates to a value of true or FALSE, and then executes a calculation to determine which of the two statements. The syntax for the conditional operator is as follows:
Ruby Range Operator:
The sequence range in Ruby is used to create a continuous value-a starting value/end value, and a range of values between the two.
In Ruby, these sequences are created using the ".." And the "..." range operator. The form of these two points establishes an inclusive range, while the form of three points creates a range that does not include the specified maximum value.
Ruby defined? Operator:
Defined is a special operator that takes the form of a method call to determine whether the expression is defined. If there is no expression defined, it returns an expression or nil that describes the solution of the string
There are many ways to use the defined operator:
Usage 1
Defined? Variable # True if variable is initialized
For example:
foo =
defined foo # => "local-variable"
defined $_ " =>" global-variable
? defined c8/># => Nil (undefined)
Usage 2
Defined? Method_call # True If A is defined
For example:
Defined? Puts # => "method"
defined? puts (bar) # => Nil (bar isn't defined here) defined
? Unpack => nil (not defined)
Usage 3
# True If a method exists that can is called with super User
defined? super
For example:
Defined? Super # => "Super" (if it can be called)
defined. Super # => Nil (if it cannot be)
Usage 4
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-no block)
Ruby "." Double colon "::" Operator:
Call a module method, referencing a constant using the module's name and a two colon, before the name of the module and the name of the period.
:: Enables unary operators, constants, instance methods, and class methods to be defined in classes or modules, accessing classes or modules from anywhere.
Keep in mind: In Ruby, classes and methods can be treated as constants. Only the prefix:: const_name expression returns the corresponding class or module object.
If there is no prefix expression, the primary 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 Glob Al 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_o NE
CONST = proc {' There '}
def where_is_my_const
:: CONST + ' inside one ' End
class I Nside_two
const = ' inside two '
def where_is_my_const
const
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
Ruby Operator Precedence
The following table lists all operators from highest priority to lowest.
Note: One of the method columns is that the operator is actually a method and may be overwritten.