Ruby Basic data Types Concise introduction _ruby topics

Source: Internet
Author: User

Generalization, the basic data types in Ruby are all class types, but for the convenience of writing and reading, it provides a simpler way to write basic data types.

Type of a value

Integers support binary, octal, decimal, hexadecimal, which dynamically determines whether an integer is a fixnum type or a bignum type, depending on the size of the integer.

Floating-point numbers support scientific notation, with at least one digit after the decimal point.

The value type inheritance diagram is as follows:

Copy Code code as follows:

Numeric
|--integer
|--fixnum
|--bignum
|--float
|--complex (Standard library)
|--bigdecimal (Standard library)
|--rational (Standard library)

Second, String

There are no characters in Ruby, only strings.

Single quote string, only ' and \ need to be escaped, the other characters keep literal meaning. (' to indicate the end of a single quote string so you need to escape, but think of one character in a single quote string \ And you need to escape. )

Double quote string, the biggest feature of the double quote string is that it can be interpolated numerically, producing double quote strings in a variety of ways.

Copy Code code as follows:

insert=100
#单引号字符串
print ' #{insert}_string ' #{insert}_string
print "\ n"
Print%q/#{insert}_string/#分隔符%q represents a single quote string
print "\ n"

#双引号字符串
Print "#{insert}_string\n" #100_string
Print%/#{insert}_string\n/#分隔符% or%q can represent double quote strings
Print%q/#{insert}_string\n/

#多行字符串
Print "The
Second line
Third line\n "#可以直接写在多行

Print <<-' multi_line ' #-can make the Terminator sign not necessarily at the beginning of the line, ' means no interpolation
The Line#{insert}
Second line
Multi_line

str1= ' Good ' #字符串是可以修改的
str1[0]= ' h '
Print str1

Iii.. Interval

The interval provides an easy way to handle the collection of objects with a continuous attribute, in order to save space only the reference of two objects in the interval is retained in memory.

Copy Code code as follows:

For I in 1..3 #闭合区间, output 123
Print I
End

print "\ n"

For i in "num1" ... "num3" #首闭后开, Output num1num2
Print I
End

Four, array

A collection of objects of various types can be accommodated.

Copy Code code as follows:

arr1=[1,2,3, "NUM1"]
ARR2=%W/1 2 3 num1/#%w and%w are character array delimiters, elements must be separated by spaces
Print arr1, "\ n", arr2, "\ n"
Print Arr1[1].class, "\ n" #Fixnum类型
Print Arr2[1].class #String类型

Five, hash list

A set of key-value pairs, widely used

Copy Code code as follows:

Hash1={1=> "A", "Second" =>2}
Print hash1["Second"]

VI. Symbols

Because the same string has a different copy in memory, the symbol type is used to save memory, and the same symbol has only one copy in memory, as well as the fact that strings and symbols are completely different types.

Copy Code code as follows:

Print "string". object_id, "\ n" #相同的字符串具有不同的id
Print "string". object_id, "\ n"
print:string.object_id, "\ n" #相同的符号具有相同的id
print:string.object_id, "\ n"

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.