Explain the arithmetic operations in Ruby, and explain the Ruby arithmetic operations.

Source: Internet
Author: User
Tags bitwise operators

Explain the arithmetic operations in Ruby, and explain the Ruby arithmetic operations.

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

Each operator (+-*/% ** & | ^ <>>& |) is abbreviated as a value assignment operator (+ =-=)
Ruby arithmetic operators:

Assume that the variable a = 10 and the variable B = 20:

Ruby comparison operator:

Assume that the variable a = 10 and the variable B = 20:

Ruby assignment operator:

Assume that the variable a = 10 and the variable B = 20:

Ruby parallel assignment:

Ruby also supports variable assignment in parallel. This allows multiple lines of Ruby code to initialize the variable. For example:

a = 10b = 20c = 30

You need to declare it more quickly and use parallel assignment:

a, b, c = 10, 20, 30

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

a, b = b, c

Ruby bitwise OPERATOR:

Bitwise operators and bitwise operators.

Assume that when a = 60 and B = 13; the binary format is as follows:

a = 0011 1100b = 0000 1101-----------------a&b = 0000 1100a|b = 0011 1101a^b = 0011 0001~a = 1100 0011

Supports bitwise operators in Ruby

Ruby logical operators:

Support for logical operators in Ruby

Assume that the variable a = 10 and the variable B = 20:

Ruby ternary operators:

Another operator is called a ternary operator. In this case, a true or false expression is calculated first, and a calculation result is executed to determine which of the two statements is used. The syntax of conditional operators is as follows:

Ruby range operators:

The sequence range in Ruby is used to create a continuous value, which constitutes the start value/end value and is within the range of values between the two.

In Ruby, create these sequences using the range operators "..." and. The two vertices form an inclusive range, while the three vertices form a range, excluding the specified maximum value.

Ruby defined? OPERATOR:

Defined is a method call in the form of a special operator to determine whether it is defined through an expression. If no defined expression exists, it returns an expression or nil that describes the string.

There are many usage defined operators:
Usage 1

defined? variable # True if variable is initialized

For example:

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

Usage 2

defined? method_call # True if a method is defined

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 userdefined? super

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:

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. Use the module name and two colons to reference a constant before the module name and the period.
: Makes the unary operator, constant, instance method and class method defined in the class or module, access the class or module from any place.
Remember: In Ruby, classes and methods can be considered as constants. Only the expression with the prefix: Const_name is required to return the corresponding class or module object.
If there is no prefix expression, the primary object class is used by default.
There are two examples:

MR_COUNT = 0    # constant defined on main Object classmodule Foo MR_COUNT = 0 ::MR_COUNT = 1  # set global count to 1 MR_COUNT = 2   # set local count to 2endputs MR_COUNT    # this is the global constantputs Foo::MR_COUNT # this is the local "Foo" constantSecond Example:CONST = ' out there'class Inside_one  CONST = proc {' in there'}  def where_is_my_CONST   ::CONST + ' inside one'  endendclass Inside_two  CONST = ' inside two'  def where_is_my_CONST   CONST  endendputs Inside_one.new.where_is_my_CONSTputs Inside_two.new.where_is_my_CONSTputs Object::CONST + Inside_two::CONSTputs Inside_two::CONST + CONSTputs Inside_one::CONSTputs Inside_one::CONST.call + Inside_two::CONST

Ruby operator priority

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

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

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.