Ruby 简明教程

Ruby - Operators

Ruby 支持丰富的运算符集,正如您对现代语言所期望的那样。大多数运算符实际上都是方法调用。例如,a + b 被解释为 a.+(b),其中变量 a 引用的对象中的 + 方法被调用,并且 b 作为其参数。

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.).

Ruby Arithmetic Operators

假设变量 a 等于 10,变量 b 等于 20,则 −

Assume variable a holds 10 and variable b holds 20, then −

Operator

Description

Example

+

Addition − Adds values on either side of the operator.

a + b will give 30

Subtraction − Subtracts right hand operand from left hand operand.

a - b will give -10

*

Multiplication − Multiplies values on either side of the operator.

a * b will give 200

/

Division − Divides left hand operand by right hand operand.

b / a will give 2

%

Modulus − Divides left hand operand by right hand operand and returns remainder.

b % a will give 0

**

Exponent − Performs exponential (power) calculation on operators.

a**b will give 10 to the power 20

Ruby Comparison Operators

假设变量 a 等于 10,变量 b 等于 20,则 −

Assume variable a holds 10 and variable b holds 20, then −

Operator

Description

Example

==

Checks if the value of two operands are equal or not, if yes then condition becomes true.

(a == b) is not true.

!=

Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

(a != b) is true.

>

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

(a > b) is not true.

<

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

(a < b) is true.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

(a >= b) is not true.

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

(a ⇐ b) is true.

<⇒

Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second.

(a <⇒ b) returns -1.

===

Used to test equality within a when clause of a case statement.

(1…​10) === 5 returns true.

.eql?

True if the receiver and argument have both the same type and equal values.

1 == 1.0 returns true, but 1.eql?(1.0) is false.

equal?

True if the receiver and argument have the same object id.

if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.

Ruby Assignment Operators

假设变量 a 等于 10,变量 b 等于 20,则 −

Assume variable a holds 10 and variable b holds 20, then −

Operator

Description

Example

=

Simple assignment operator, assigns values from right side operands to left side operand.

c = a + b will assign the value of a + b into c

+=

Add AND assignment operator, adds right operand to the left operand and assign the result to left operand.

c += a is equivalent to c = c + a

-=

Subtract AND assignment operator, subtracts right operand from the left operand and assign the result to left operand.

c -= a is equivalent to c = c - a

*=

Multiply AND assignment operator, multiplies right operand with the left operand and assign the result to left operand.

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

/=

Divide AND assignment operator, divides left operand with the right operand and assign the result to left operand.

c /= a is equivalent to c = c / a

%=

Modulus AND assignment operator, takes modulus using two operands and assign the result to left operand.

c %= a is equivalent to c = c % a

**=

Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand.

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

Ruby Parallel Assignment

Ruby 也支持变量的并行赋值。这允许使用单行 Ruby 代码初始化多个变量。例如 −

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

Ruby Bitwise Operators

按位运算符处理位并执行逐位运算。

Bitwise operator works on bits and performs bit by bit operation.

假设 a = 60;和 b = 13;现以二进制格式表示如下 −

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

Ruby 语言支持以下按位运算符。

The following Bitwise operators are supported by Ruby language.

Operator

Description

Example

&

Binary AND Operator copies a bit to the result if it exists in both operands.

(a & b) will give 12, which is 0000 1100

Binary OR Operator copies a bit if it exists in either operand.

(a

b) will give 61, which is 0011 1101

^

Binary XOR Operator copies the bit if it is set in one operand but not both.

(a ^ b) will give 49, which is 0011 0001

~

Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.

(~a ) will give -61, which is 1100 0011 in 2’s complement form due to a signed binary number.

<<

Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.

a << 2 will give 240, which is 1111 0000

>>

Ruby Logical Operators

Ruby 语言支持以下逻辑运算符

The following logical operators are supported by Ruby language

假设变量 a 等于 10,变量 b 等于 20,则 −

Assume variable a holds 10 and variable b holds 20, then −

Operator

Description

Example

and

Called Logical AND operator. If both the operands are true, then the condition becomes true.

(a and b) is true.

or

Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true.

(a or b) is true.

&&

Called Logical AND operator. If both the operands are non zero, then the condition becomes true.

(a && b) is true.

Called Logical OR Operator. If any of the two operands are non zero, then the condition becomes true.

(a

b) is true.

!

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

!(a && b) is false.

not

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false.

Ruby Ternary Operator

还有一个称为三元运算符的运算符。它首先对一个表达式求值以获得真或假的值,然后根据求值结果执行两个给定语句中的一个。条件运算符具有以下语法:

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 −

Operator

Description

Example

? :

Conditional Expression

If Condition is true ? Then value X : Otherwise value Y

Ruby Range Operators

Ruby 中的序列范围用于创建一系列连续值,包括一个起始值、一个结束值和介于两者之间的值范围。

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.

在 Ruby 中,这些序列使用“..”和“···”范围运算符创建。两点形式创建一个包含范围,而三点形式创建一个排除指定高值的范围。

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.

Operator

Description

Example

..

Creates a range from start point to end point inclusive.

1..10 Creates a range from 1 to 10 inclusive.

…​

Creates a range from start point to end point exclusive.

1…​10 Creates a range from 1 to 9.

Ruby defined? Operators

defined? 是一个特殊运算符,它采用方法调用的形式来确定传入的表达式是否已定义。它返回表达式的描述字符串,如果表达式未定义,则返回 nil。

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.

defined? 运算符有以下各种使用方法

There are various usage of defined? Operator

Usage 1

defined? variable # True if variable is initialized

For Example

For Example

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

Usage 2

defined? method_call # True if a method is defined

For Example

For Example

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

Usage 3

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

For Example

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

For Example

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

Ruby Dot "." and Double Colon "::" Operators

在模块方法名称前加上模块名称和一个句点来调用它,并且使用模块名称和两个冒号来引用常量。

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.

在 Ruby 中,类和方法也可以视为常量。

Remember in Ruby, classes and methods may be considered constants too.

你只需要为 :: Const_name 添加前缀,返回适当的类或模块对象。

You need to just prefix the :: Const_name with an expression that returns the appropriate class or module object.

如果没有使用前缀表达式,则默认使用主 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

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 Operators Precedence

下表列出了从高到低优先级的运算符。

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

Method

Operator

Description

Yes

::

Constant resolution operator

Yes

[ ] [ ]=

Element reference, element set

Yes

**

Exponentiation (raise to the power)

Yes

! ~ + -

Not, complement, unary plus and minus (method names for the last two are +@ and -@)

Yes

* / %

Multiply, divide, and modulo

Yes

+ -

Addition and subtraction

Yes

>> <<

Right and left bitwise shift

Yes

&

Bitwise 'AND'

Yes

^

Bitwise exclusive `OR' and regular `OR'

Yes

⇐ < > >=

Comparison operators

Yes

<⇒ == === != =~ !~

Equality and pattern match operators (!= and !~ may not be defined as methods)

&&

Logical 'AND'

Logical 'OR'

.. …​

Range (inclusive and exclusive)

? :

Ternary if-then-else

= %= { /= -= +=

= &= >>= <⇐ *= &&=

= **=

Assignment

defined?

Check if specified symbol defined

not

Logical negation

or and

Logical composition

NOTE − 方法列包含“是”的运算符实际上是方法,因此可以被覆盖。

NOTE − Operators with a Yes in the method column are actually methods, and as such may be overridden.