Ruby Basic knowledge of data types _ruby topics

Source: Internet
Author: User
Tags arithmetic exponent operator hash numeric new set numeric value

First, the numeric type

(1) Integer
There are two types of integers. If it is within 31 bits (four bytes), it is a Fixnum instance. If it exceeds, it is an instance of Bignum.

#Integer Integer The following are some integer literals
#Literal (literal): the value, numeric value, bool value, string, etc. that can be seen in the code are called literals
#Such as the following 0,1_000_000,0xa, etc.
a1 = 0
#Integer with thousandths
a2 = 1_000_000
#Other hexadecimal representation
a3 = 0xa
puts a1, a2
puts a3
#puts print is to print characters to the console, including puts with carriage return and line feed
= begin
This is a comment called: Embedded Document Comment
Similar to / ** / in C #
= end

(2) Floating point type

#Float
f1 = 0.0
f2 = 2.1
f3 = 1000000.1
puts f3

There is a rounding error in floating point, for example: 0.4-0.3 == 0.1
This is not the same. The same is true in C # (the same is true for IEEE-754 floating point). Because many floating point numbers are represented by an approximation. For example 0.1, it is just infinitely close to 0.1. Because 0.1 cannot be accurately expressed in binary. But 0.5 can.

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 operator: +-* /; exponent operator is **
The exponent need not be an integer, for example

#Exponential arithmetic
puts 2 ** (1/4) # 1 and 4 have a quotient of 0, then 2 to the power of 0 is 1
puts 16 ** (1 / 4.0) # The quotient of # 1 and 4.0 is 0.25 (one-quarter), and then the fourth root

(Two) string type
The string type can be represented by single or double quotes. Two tabular methods are recommended to use double quotes: double quotes escape all characters; literals in double quotes can contain expressions.
The string interpolation format is: # {}

name = "Ruby"
puts name
puts "# {name +", ok "}"

String delimiter
String literals can be defined by string delimiters.
% q for single quote rule
% Q is used for double quote rule
The delimiters appear in pairs, for example: (), [], {}, two! !! . If used! As a delimiter, the literal appears again! No, then, you need \! To escape. Of course, if a pair of delimiters appears in the literals (actually as operators), then no escaping is needed.


s1 =% Q [this '' / ssss123]
puts s1
#There is a delimiter in the literal, and in general, it is necessary to do an escape operation
s2 =% Q! this '' / ssss123 \! \ !!
puts s2
#Literal pairs of delimiters appear in the literal, you can do without escaping
s3 =% Q (2 * (1 + 1))
puts s3

In large string literals, there is no guarantee that delimiters will not appear. Support header document in ruby. That is, by defining the demarcation string to define the literal quantity, the success rate can be greatly guaranteed.
It is defined by << or <<<-immediately next to the header, and the end segment is on a separate line. If defined by <<-, a blank character can be added before the end line. E.g:

# Demarcation string
s1 = << Header
sdfie '' '' ////// []
Header
puts s1

String manipulation
(1) Use + to perform string concatenation. For those that are not strings, you need to_s method for display conversion before they can be used for connection. Note that the connected string will create a new object and return.
(2) Use << to connect character strings. This operator concatenates the strings and modifies the strings on the left without creating new objects.
(3) Use * to indicate repeating the left character string,
(4) String interception
Use [] to access the substring in the string. Strings can be viewed as character arrays. If the index is negative, the characters are taken from right to left. (The difference between returning by [] access in 1.8 and 1.9, which is more in line with habit in 1.9)
Through the [] index access for assignment operations, you can replace characters.
Access through two values in [], you can intercept substrings
(5) Intercept substrings by range.
The two expressed by range are both indexes, which is different from the one separated by comma in [] for index and the other for length.
[..]or[…]
Two of the points are inclusive.
(5) Determine the inclusion relationship by indexing the string
["String '" to determine whether this substring is included
(3) Character type
Character type passed? + Character to define.

(Four) array

Array literals are defined by [] separated by commas, and range definitions are supported. At the same time, the array literal references% w,% W delimiters similar to% q,% Q. They are separated by spaces.
(1) The array is accessed through the [] index
Similar to strings, data is accessed through indexes. If there are two values, one represents the index and the other represents the number of elements
(2) Insert, delete, and replace elements by assignment
(3) Merge and delete elements by + and-signs, and the set appears as a new set
(4) Add elements to the original data by <<
(5) Repeat array elements by *
(6) Do union and intersection operations with | and & symbols (note the order)
(5) Hash type
Hash literals are defined by comma-separated kv pairs, included between curly braces, kv pairs are defined with =>

#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) Type of scope
The .. or ... symbol defines the range type, which is sequential.
(7) true, false, nil
To compare nil values, you can pass:
Ojb == nil or
Obj.nil?
(8) Object identification, object class and type
Object identification can be passed in 1.9:
__id__ get, or object_id
 
Object class:

Obj.class == String or
Obj.instance_of? String
 
x1 = "ok"
puts x1.class == String
puts x1.instance_of? String

You can also determine whether a type of instance is: is_a? Or ===

x1 = "ok"
puts x1.is_a? String
puts x1 === String

(9) Object freezing and pollution
By freezing the object, the frozen object will be immutable (all internal states are immutable). If the class is frozen, the class cannot add methods.
By taint polluting objects, the polluted objects will become the source of pollution (the objects derived from it are all polluted). Untaint to decontaminate.

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.