Explain the arithmetic in Ruby

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise bitwise operators constant logical operators range

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:


1 2 3 A = ten b = c = 30

Need to declare more quickly, using parallel assignment:


1 A, b, C = 10, 20, 30

It is also useful to exchange values for two variables in parallel assignment:


1 A, B = B, c

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:


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 00 11

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


1 Defined? Variable # True if variable is initialized

For example:


1 2 3 4 Foo = defined? Foo # => "local-variable" defined? $_ # => "global-variable" defined? Bar # => Nil (undefined)

Usage 2


1 Defined? Method_call # True If A is defined

For example:


1 2 3 Defined? Puts # => "method" defined? Puts (bar) # => nil (bar isn't defined here) defined? Unpack # => nil (not defined)

Usage 3


1 2 # True If a method exists that can is 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, 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:


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_cou NT = 2 # Set local count to 2-puts Mr_count # This is the global constant puts Foo::mr_count # ' is the ' local ' Foo ' Constant Second example:const = ' out there ' class Inside_one CONST = proc {' In there '} def where_is_my_const:: CON ST + ' inside one ' End-class inside_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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.