Explain arithmetic operations in ruby in detail

Time:2022-5-26

 RubyA modern language that supports a rich set of operators. Most operators are actually method calls. For example, the of a + B is interpreted as a,
+(b) The method of the object referenced by the variable is called a method with B as its parameter.

For each operator (+ – * /% * * & | ^ < > & & |), there are corresponding abbreviations of assignment operators (+ = – = etc.)
Ruby arithmetic operators:

Assuming variable a = 10 and variable b = 20:

Explain arithmetic operations in ruby in detail

Ruby comparison operator:

Assuming variable a = 10 and variable b = 20:

Explain arithmetic operations in ruby in detail

Ruby assignment operator:

Assuming variable a = 10 and variable b = 20:

Explain arithmetic operations in ruby in detail

Ruby parallel assignment:

Ruby also supports variables that are assigned in parallel. This allows multiple lines of ruby code to initialize variables. For example:

?
1
2
3
a = 10
b = 20
c = 30

For faster declarations, use parallel assignments:

?
1
a, b, c = 10, 20, 30

It is also useful to exchange the values of two variables in parallel:

?
1
a, b = b, c

Ruby bitwise operator:

Bit operator bit and bit operation execution bit.

Suppose a = 60 and B = 13; Now in binary format, it will be as follows:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
a = 0011 1100
 
b = 0000 1101
 
-----------------
 
a&b = 0000 1100
 
a|b = 0011 1101
 
a^b = 0011 0001
 
~a = 1100 0011

Bitwise operators supporting Ruby language

Explain arithmetic operations in ruby in detail

Ruby logical operators:

Support for logical operators in Ruby language

Assuming variable a = 10 and variable b = 20:

Explain arithmetic operations in ruby in detail

Ruby ternary operator:

There is also an operator called the ternary operator. This first evaluates an expression to true or false, and then executes a calculation to determine which of the two statements. The syntax of the conditional operator is as follows:

Explain arithmetic operations in ruby in detail

Ruby range operator:

The sequence range in ruby is used to create continuous values – which make up the start value / end value and are within the range of values between them.

In ruby, these sequences are created using “..” And “…” Range operator. The two point form creates an inclusive range, while the three point form creates a range that does not include the specified maximum.

Explain arithmetic operations in ruby in detail

Ruby defined? Operator:

Defined is a method call in the form of a special operator to determine whether it is defined by an expression. If there is no defined expression, it returns an expression or nil solved by the description string

There are many uses of the defined operator:
Usage 1

?
1
defined? variable # True if variable is initialized

For example:

?
1
2
3
4
foo = 42
defined? foo  # => "local-variable"
defined? $_   # => "global-variable"
defined? bar  # => nil (undefined)

Usage 2

?
1
defined? method_call # True if a method is defined

For example:

?
1
2
3
defined? puts    # => "method"
defined? puts(bar)  # => nil (bar is not defined here)
defined? unpack   # => nil (not defined here)

Usage 3

?
1
2
# True if a method exists that can be called with super user
defined? super

For example:

?
1
2
defined? super   # => "super" (if it can be called)
defined? super   # => nil (if it cannot be)

Usage 4

?
1
defined? yield  # True if a code block has been passed

For example:

?
1
2
defined? yield  # => "yield" (if there is a block passed)
defined? yield  # => nil (if there is no block)

Ruby “.” Double colon ‘::’ operator:

Call a module method, reference a constant before its name through the module name and period, and use the module name and two colons.
:: make unary operators, constants, instance methods and class methods defined in the class or module, and access the external class or module from anywhere.
Remember: in ruby, classes and methods can be treated as constants. Only the prefix:: const is required_ The expression of name returns the corresponding class or module object.
If there is no prefix expression, the main object class is used by default.
Here are two examples:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

Ruby operator priority

The following table lists all operators from highest priority to lowest priority.

Explain arithmetic operations in ruby in detail

Note: the method column is an operator, which is actually a method, so it may be rewritten.