Ruby operator


Release date:2023-10-26 Update date:2023-10-26 Editor:admin View counts:223

Label:

Ruby operator

Ruby supports a rich set of operators. Most operators are actually method calls. For example, a + b are interpreted as a.+(b) where the + method pointing to the variable an is called and b is used as a parameter tothe method call.

For each operator (+- * / % * * &| ^ < > > & &| |), all have an acronym assignment operator (+ =-=, etc.).

Ruby arithmetic operator

Assuming that the value of variable an is 10 and the value of variable b is 20, then:

Operator

Description

Example

+

Addition-adds the operands on both sides of the operator

A + b will get 30

-

Subtraction-subtracts the left Operand from the right Operand

A-b will get-10

*

Multiplication-multiplies the operands on both sides of the operator

A * b will get 200

/

Division-divide the left Operand by the right Operand

B / a will get 2.

%

Modulus-divide the left Operand by the right Operand and return the remainder

B a will get 0

**

Index-perform index calculation

Will get 10 to the power of 20.

Ruby comparison operator

Assuming that the value of variable an is 10 and the value of variable b is 20, then:

Operator

Description

Example

==

Check whether the values of the two operands are equal, and if so, the condition is true.

(a = = b) is not true.

!=

Check whether the values of the two operands are equal, and if not, the condition is true.

(a! = b) true.

>

Check whether the value of the left Operand is greater than that of the right Operand, and if so, the condition is true.

(a > b) not true.

<

Check whether the value of the left Operand is less than the value of the right Operand, and if so, the condition is true.

(a < b) is true.

>=

Check whether the value of the left Operand is greater than or equal to the value of the right Operand, and if so, the condition is true.

(a > = b) not true.

<=

Check whether the value of the left Operand is less than or equal to the value of the right Operand, and if so, the condition is true.

(a < = b) is true.

<=>

Joint comparison operator. Returns 0 if the first Operand is equal to the second Operand, 1 if the first Operand is greater than the second Operand, and-1 if the first Operand is less than the second Operand.

(a < = > b) returns-1.

===

For testing? case? Equality within the when clause of a statement.

(1. 10) = 5 returns true.

.eql?

Returns true if the receiver and parameter have the same type and equal value.

1 = = 1. 0 returns true, but 1.eql returns false.

equal?

Returns true if the receiver and parameter have the same object id.

If aObj is a copy of bObj, then aObj = = bObj returns true,a.equal?bObj returns false, but a.equal?aObj returns true.

Ruby assignment operator

Assuming that the value of variable an is 10 and the value of variable b is 20, then:

Operator

Description

Example

=

A simple assignment operator that assigns the value of the right Operand to the left Operand

C = a + b will assign the value of a + b to c

+=

Add the assignment operator to assign the result of the right Operand plus the left Operand to the left Operand

C + = an is equivalent to c = c + a

-=

Subtract and assign the result of the left Operand minus the right Operand to the left Operand

C-= an is equivalent to c = c-a

*=

Multiply and assign the operator to assign the result of the right Operand multiplied by the left Operand to the left Operand

c *= a equivalent to c = c * a

/=

Divide and assign the operator to assign the result of dividing the left Operand by the right Operand to the left Operand

c /= a equivalent to c = c / a

%=

Find the module and assignment operator, and find the module assignment of two operands to the left Operand

c %= a equivalent to c = c % a

**=

Exponential and assignment operator, performs exponential calculation, and assigns values to the left Operand

c **= a equivalent to c = c ** a

Ruby parallel assignment

Ruby also supports parallel assignment of variables. This allows multiple variables to be initialized with one line of Ruby code. For example:

a=10b=20c=30

Parallel assignments can be used to declare more quickly:

a,b,c=10,20,30

Parallel assignments are also useful when exchanging the values of two variables:

a,b=b,c

Ruby bit operator

The bit operator acts on the bit and performs the operation bit by bit.

Suppose that if a = 60 , and b = 13 now in binary format, they areas follows:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

The following table lists the bit operators supported by Ruby.

Operator

Description

Example

&

If it exists in both operands, the binary AND operator copies a bit to the result.

(a & b) will get 12, that is, 0000 1100

|

If it exists in any Operand, the binary OR operator copies a bit into the result.

(a | b) will get 61, that is, 0011 1101

^

If it exists in one of the operands but not in both operands, the binary XORoperator copies a bit to the result.

(a ^ b) will get 49, that is, 0011 0001

~

The binary complement operator is a unary operator with a “flip” bit effect, that is, 0 becomes 1, 1 becomes 0.

(~ a) will get-61, that is, 1100 0011, a complement of signed binary numbers.

<<

Binary left shift operator. The value of the left Operand moves the number of digits specified by the right Operand to the left.

A << 2 will get 240, that is, 1111 0000

>>

Binary right shift operator. The value of the left Operand moves the number of digits specified by the right Operand to the right.

A >> 2 will get 15, that is, 0000 1111

Ruby logical operator

The following table lists the logical operators supported by Ruby.

Assuming that the value of variable an is 10 and the value of variable b is 20, then:

Operator

Description

Example

and

It is called logic and operator. If both operands are true, the condition istrue.

(an and b) is true.

or

It is called logic or operator. If either of the two operands is non-zero, the condition is true.

(an or b) is true.

&&

It is called logic and operator. If both operands are nonzero, the conditionis true.

(a && b) is true.

||

It is called logic or operator. If either of the two operands is non-zero, the condition is true.

(a || b) is true.

!

Is called a logical non-operator. Used to reverse the logical state of operands. If the condition is true, the logical non-operator makes it false.

! (a && b) is false.

not

Is called a logical non-operator. Used to reverse the logical state of operands. If the condition is true, the logical non-operator makes it false.

Not (a && b) is false.

Ruby ternary operator

There is more than one operation called a ternary operator. The first evaluates the true or false value of the expression, and then decides to execute one of the next two statements based on this result. The syntax of the conditional operator is as follows:

Operator

Description

Example

?:

Conditional expression

What if the conditions are true? Then the value is X: otherwise the value is Y

Ruby range operator

In Ruby, sequence ranges are used to create a series of consecutive values-including the start value, the end value (as the case may be), and the values between them.

In Ruby, these sequences use “..” and “.” Created by the range operator.The range created in the two-point form contains the start and end values, and the range created in the three-point form contains only the start value and not the end value.

Operator

Description

Example

..

Create a range from the start point to the end point (including the end point)

1..10 create a range from 1 to 10

Create a range from the start point to the end point (excluding the end point)

1….10 create a range from 1 to 9

Ruby defined? operator

defined? is a special operator that determines whether the passed expression is defined in the form of a method call. It returns the description string of the expression, or if the expression is not defined nil .

The following is defined? various uses of the operator:

Usage 1

defined?variable#True if variable has already been initialized

For example:

foo=42defined?foo#=> "local-variable"defined?$\_#=>
"global-variable"defined?bar#=> nil(undefined)

Usage 2

defined?method_call#True if the method is already defined

For example:

defined?puts#=> "method"defined?puts(bar)#=> nil(Bar is not defined here)
defined?unpack#=> nil(Undefined here)

Usage 3

#If there are methods that can be called by super users; otherwise Truedefined?super

For example:

defined?super#=> "super"(If it can be called)defined?super#=>
nil(If it cannot be called)

Usage 4

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

For example:

defined?yield#=> "yield"(If a block has been passed)defined?yield#=>
nil(If the block is not passed)

Ruby dot operator “.” And the double colon operator “::”

You can add the class or module name and before the method name. To call a method in a class or module. You can use the class or module name and two colons:: to refer to constants in the class or module.

:: is a unary operator that allows constants, instance methods, and class methods to be defined within a class or module, which can be accessed from anywhere outside the class or module.

Remember: in Ruby, classes and methods can also be treated as constants.

You just need to precede the constant name of the expression with the :: prefix, the appropriate class or module object is returned.

If :: if the previous expression is the name of the class or module, the corresponding constant value within the class or module is returned; if :: if there is no prefix expression before it, the main is returned. Object the corresponding constant value in the class. .

Here are two examples:

MR_ COUNT=0 # defined in the main object
Constants on class moduleFooMR_ COUNT=0:: MR_ COUNT=1 # Set global count to
1MR_ COUNT=2 # Set local count to
2endputsMR_ COUNT # This is the global constant putsFoo:: MR_ COUNT # This is the local constant for 'Foo'

The second example:

CONST='out there'classInside_oneCONST=proc{'in
there'}defwhere_is_my_CONST::CONST+'inside
one'endendclassInside_twoCONST='inside
two'defwhere_is_my_CONSTCONSTendendputsInside_one.new.where_is_my_CONSTputsInside_two.
new.where_is_my_CONSTputsObject::CONST+Inside_two::CONSTputsInside_two::CONST+CONSTputsInside_one::
CONSTputsInside_one::CONST.call+Inside_two::CONST

Precedence of the Ruby operator

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

Method

Operator

Description

Yes

::

Constant parsing operator

Yes

[ ] [ ]=

Element references, element collections

Yes

**

Index

Yes

! ~ + -

No, complement, unary plus, unary minus (the last two methods are called + @and-@)

Yes

* / %

Multiplication, division, modulus

Yes

+ -

Addition and subtraction

Yes

>> <<

Shift to the right, shift to the left

Yes

&

Bit and

Yes

^ |

Bit heterOR, bit or

Yes

<= < > >=

Comparison operator

Yes

<=> == === != =~ !~

Equality and pattern matching operators (! = and! ~ cannot be defined as methods)

&&

Logic vs.

||

Logical OR

.. ...

Range (inclusive, non-inclusive)

??:

Ternary if-then-else

= %= { /= -= += |= &= >>= <<= *= &&= ||= **=

Assignment

defined?

Check whether the specified symbol is defined

not

Logical negation

or and

Logical composition

The operator identified as yes in the method column is actually a method andcan be overloaded.

Powered by TorCMS (https://github.com/bukun/TorCMS).