home PYTHONJAVA
 

Ruby Operators

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

For each operator (+ - * / % ** & | ^ << >> && ||), there is a corresponding abbreviation assignment operator (+= - = and so on).

Ruby arithmetic operators

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

OperatorsDescriptionInstances
+Addition - Add the operands on both sides of the operator a + b will get 30
-Subtraction - Subtract the left operand from the right operand a - b will get -10
*Multiplication - Multiply 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
%Modification - Divide the left operand by the right operand and return the remainder b % a will get 0
**Index - Execution Index Calculation a**b will get 10 20th power

Ruby comparison operator

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

OperatorsDescriptionInstances
==Checks if the values of the two operands are equal, and if they are equal, the condition is true. (a == b) is not true.
!=Checks if the values of the two operands are equal. If they are not equal, the condition is true. (a != b) is true.
>Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. (a > b) Not true.
<Checks if 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.
>=Checks if 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) is not true.
<=Checks if 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 operators. Returns 0 if the first operand is equal to the second operand, and returns 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.
=== is used to test equality within the when clause of the case statement. (1...10) === 5 returns true.
.eql? Returns true if the receiver and parameters have the same type and equal value. 1 == 1.0 returns true, but 1.eql?(1.0) returns false.
equal? Returns true if the receiver and parameters 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 the value of variable a is 10 and the value of variable b is 20, then:

OperatorsDescriptionInstances
=Simple assignment operator assigns the value of the right operand to the left operand c = a + b will assign the value of a + b to c
+=Add and assign the operator, assign the result of the right operand plus the left operand to the left operand c += a is equivalent to c = c + a
-= minus the assignment operator, assigning the left operand minus the result of the right operand to the left operand c -= a is equivalent to c = c - a
*=Multiply and assign operator, assign the result of multiplying the right operand to the left operand to the left operand c *= a is equivalent to c = c * a
/=In addition to the assignment operator, the result of dividing the left operand by the right operand is assigned to the left operand c /= a is equivalent to c = c / a
%= modulo and assignment operator, find the modulo assignment of two operands to the left operand c %= a is equivalent to c = c % a< /td>
**=index and assignment operator, perform exponential calculation, and assign to left operand c **= a is equivalent to c = c ** a< /td>

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 = 10 b= 20< Span class="hl-code"> c= 30< /div>

Use parallel assignment to declare faster:

a, b, c = 10, 20, 30

Parallel assignment is also useful when exchanging the values of two variables:

a, b = b, c

Ruby bit operator

The

bitwise operator acts on bits and performs operations bit by bit.

Assume that if a = 60 and b = 13, now in binary format, they are 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 table lists the bitwise operators supported by Ruby.

OperatorsDescriptionInstances
& If both operands exist in both operands, the binary AND operator copies one bit into the result. (a & b) will get 12, which is 0000 1100
| If present in any operand, the binary OR operator copies one bit into the result. (a | b) will get 61, which is 0011 1101
^ If it exists in one of the operands but not in both operands, the binary XOR operator copies one bit into the result. (a ^ b) will get 49, which is 0011 0001
~ The two's complement operator is a unary operator with a "flip" bit effect, ie 0 becomes 1, and 1 becomes 0. (~a ) will get -61, which is 1100 0011, a complement of a signed binary number.
<< Binary left shift operator. The value of the left operand moves to the left by the number of digits specified by the right operand. a << 2 will get 240, which is 1111 0000
>>Binary right shift operator. The value of the left operand moves to the right by the number of digits specified by the right operand. a >> 2 will get 15, which is 0000 1111

Ruby logical operators

The following table lists the logical operators supported by Ruby.

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

OperatorsDescriptionInstances
and is called a logical AND operator. The condition is true if both operands are true. (a and b) is true.
or is called a logical OR operator. The condition is true if either of the two operands is non-zero. (a or b) is true.
&& is called a logical AND operator. The condition is true if both operands are non-zero. (a && b) is true.
|| is called a logical OR operator. The condition is true if either of the two operands is non-zero. (a || b) is true.
! is called a logical non-operator. The logical state used to reverse the operands. A logical non-operator will make it false if the condition is true. !(a && b) is false.
not is called a logical non-operator. The logical state used to reverse the operands. A logical non-operator will make it false if the condition is true. not(a && b) is false.

Ruby ternary operator

There is more than one operation called the ternary operator. The first evaluates the true and false values ​​of the expression, and then decides to execute one of the two statements following the result. The syntax of the conditional operator is as follows:

OperatorsDescriptionInstances
? :conditional expression If the condition is 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 start values, end values ​​(as appropriate), and values ​​between them.

In Ruby, these sequences are created using the ".." and "..." range operators. The two-point form creates a range of start and end values, and the three-point form creates a range that only contains the start value and no end value.

OperatorsDescriptionInstances
..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 (without 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 nil if the expression is undefined.

The following are the various uses of the defined operator:

Usage 1

defined? variable #If variable has been initialized,Then True

Example:

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

Usage 2

defined? method_call # If the method is already defined, then True

Example:

defined? puts # => "method" defined? puts(bar) # => nil(在这里 bar 未定义) defined? unpack # => nil(not defined here)

Usage 3

# If there is a method that can be called by the super user, then True defined? super

Example:

defined? super # => "super"(if can be called) defined? super # => nil(if not callable)

Usage 4

defined? yield # If the code block has been passed, it is True

Example:

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

Ruby dot operator "." and double colon operator "::"

You can call methods in a class or module by prefixing the method name with the class or module name and .. You can refer to a constant in a class or module using the class or module name and two colons ::.

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

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

You only need to prefix the constant name of the expression with the :: prefix to return the appropriate class or module object.

If the expression before :: is a class or module name, return the corresponding constant value in that class or module; if : : There is no prefix expression before, returning the corresponding constant value in the main Object class. .

The following are two examples:

Second instance:

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

Priority of the Ruby operator

The following table lists all operators from high to low according to their priority.

MethodOperatorDescription
Yes :: Constant Parsing Operator
Yes [ ] [ ]= Element reference, element collection
Yes ** index
Yes ! ~ + - Non, Complement, One Dollar Plus, One Dollar Subtraction (the last two methods are named +@ and -@)
Yes * / % multiplication, division, modulo
Yes + - Addition and Subtraction
Yes >> << bit shifts right and left shifts
Yes & bit and
Yes ^ | XOR, bit or
Yes <= < > >= Compare operator
Yes <=> == === != =~ !~ equality and pattern matching operators (!= and !~ cannot be defined as methods)
&& Logic and
|| Logic or
.. ... scope (included, not included)
? : ternary if-then-else
= %= { /= -= += |= &= >>= <<= *= &&= ||= **= assignment
defined? Check if the specified symbol is defined
not Logical negation
or and Logical composition

Note: Operators identified as is in the method column are actually methods and can therefore be overloaded.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.