Basic Ruby knowledge: Data Type and basic ruby knowledge

Source: Internet
Author: User

Basic Ruby knowledge: Data Type and basic ruby knowledge

I. numeric type

(1) Integer
There are two types of integer types. If the value is within 31 characters (four bytes), it is a Fixnum instance. If the value is exceeded, it is a Bignum instance.
Copy codeThe Code is as follows:
# Integer: The following are some Integer literal values.
# Literal: The values, values, bool values, and strings that can be seen in the Code are called literal values.
# For example, the following 0, 1 _ 000_000, and 0xa
A1 = 0
# Integer With a kilobytes of Characters
A2 = 0000000_000
# Other hexadecimal Representation
A3 = 0xa
Puts a1, a2
Puts a3
# Puts print all characters to the console, where puts carries a carriage return line break
= Begin
This is an annotation called an embedded document annotation.
Similar to/**/in C /**/
= End

(2) floating point
Copy codeThe Code is as follows:
# Floating Point Type
F1 = 1, 0.0
F2 = 1, 2.1
F3 = 1000000.1.
Puts f3

The floating point has an integer error, for example, 0.4-0.3 = 0.1.
This is not the case. The same is true in C #(IEEE-754 floating point is used ). Because many floating point numbers are expressed with an approximate value. For example, 0.1 is just infinitely close to 0.1. Because 0.1 cannot be accurately expressed in binary format. But 0.5 is acceptable.
Copy codeThe Code is as follows:
Puts 0.3-0.2 = 0.1 # false
Puts 0.8-0.3 = 0.5 # true
Puts 0.8-0.7 = 0.1 # false

(3) arithmetic operations
Addition, subtraction, multiplication, and Division operators: +-*/; exponential operators **
The exponent does not have to be an integer, for example
Copy codeThe Code is as follows:
# Exponential Arithmetic
Puts 2 ** (1/4) # The vendors of 1 and 4 are 0, and the 0 times of 2 are 1.
Puts 16 ** (1/4. 0) # The vendors of 1 and 4.0 are 0.25 (1/4), and then the quad-power Root

(2) string type
String type can be expressed in single or double quotation marks. Double quotation marks are recommended for two table-based methods: Double quotation marks can escape all characters; the literal quantity in double quotation marks can contain expressions.
String interpolation format :#{}
Copy codeThe Code is as follows:
Name = "Ruby"
Puts name
Puts "# {name +", OK "}"

String delimiter
You can use string delimiters to define the string literal volume.
% Q for single quotes
% Q used for double quotation mark rules
The delimiters appear in pairs, such as: (), [], {}, and two !!. If yes! As the separator, the literal appears again! , So \! To escape. Of course, if the literal contains a pair of delimiters (actually used as operators), you do not need to escape them.
Copy codeThe Code is as follows:
# Delimiters
S1 = % Q [this ''/ssss123]
Puts s1
# There are delimiters in the literal volume. Generally, escape operations are required.
S2 = % Q! This ''/ssss123 \! \!!
Puts s2
# A pair of delimiters appears in the literal. Escape is not allowed.
S3 = % Q (2*(1 + 1 ))
Puts s3

In a large string literal volume, it is not guaranteed that the delimiter does not appear. Header document is supported in ruby. That is, defining the split string to define the literal quantity can greatly guarantee the success rate.
It is defined by <or <-header, and the end segment is a single row. If it is defined by <-, a blank character is added before the end line. For example:
Copy codeThe Code is as follows:
# Dividing string
S1 = <Header
Sdfie ''' // []
Header
Puts s1

String operation
(1) Use the plus sign (+) for string connection. The to_s method is required to display and convert strings before they can be used for connection. Note that a new object is returned for the connected string.
(2) Use the <symbol for string connection. This operator concatenates a string, which is a string on the left without creating a new object.
(3) Use the "*" sign to repeat the string on the left,
(4) string Truncation
Use [] to access the substring in the string. Strings can be viewed as character arrays. If the index is negative, take the character from right to left. (In 1.8 and 1.9, different responses are returned through [], and the results in 1.9 are more in line with habits)
Assign values through [] index access, which can replace characters.
Access by using two values in [] to intercept sub-strings
(5) truncate the substring through range.
The two indicated by range are indexes, which are different from the one separated by commas (,) for index and the other for length.
[..] Or […]
The two vertices contain intervals.
(5) use index strings to determine the inclusion relationship
["String"] is used to determine whether the substring is contained.
(3) character type

What is the character type? + Characters.

(4) array

The array literal is defined by commas (,) in [], and range is supported. In addition, the array literal references the % w and % W delimiters similar to % q and % Q. Separated by spaces.
(1) accessing arrays through [] Indexes
Similar to a string, data is accessed through an index. If two values exist, one indicates the index and the other indicates the number of elements.
(2) Insert, delete, and replace elements by assigning values
(3) Merge and delete elements through the plus (+) and use the set as the new set.
(4) append an element to the original data through the <symbol
(5) Repeat the array element through the * sign
(6) perform union and intersection operations through | and & symbols (pay attention to the Order)
(5) hash type
The hash literal is defined by kv pairs separated by commas (,). It is included between curly braces and kv pairs are defined by =>.
Copy codeThe Code is as follows:
# Hash
H1 = {"a1" => 1, "a2" => 2}
H2 = {: a1 => 3,: a2 => 4}
H3 = {a1: 5, a2: 6}
 
Puts h1 ["a1"]
Puts h2 [: a1]
Puts h3 [: a1]

(6) range type
Through... or... A symbol defines the range type, which is sequential.
(7) true, false, nil
To compare the nil value, you can use:
Ojb = nil or
Obj. nil?
(8) Object ID, object class, and type
The object ID in 1.9 can be passed through:
_ Id _, or object_id
 
Object class:
Copy codeThe Code is as follows:
Obj. class = String or
Obj. instance_of? String
 
X1 = "OK"
Puts x1.class = String
Puts x1.instance _? String

To determine whether a type of instance can also be passed: is_a? Or ===
Copy codeThe Code is as follows:
X1 = "OK"

Puts x1.is _? String
Puts x1 = String

(9) object freezing and pollution
The freeze object is used to freeze objects. Frozen objects cannot be changed (all internal states cannot be changed ). If the class is frozen, you cannot add a method to the class.
If a taint object is contaminated, the contaminated object becomes a source of pollution (all objects originating from it are contaminated ). Untaint is used to remove pollution.

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.