Ruby basic data types: ruby Data Types

Source: Internet
Author: User

Ruby basic data types: ruby Data Types

Generally, the basic data types in ruby are all class types, which provide a concise writing method for the basic data types for ease of writing and reading.

I. numeric type

The integer type can be binary, octal, decimal, or hexadecimal. The integer type is Fixnum or Bignum dynamically determined based on the integer size.

Floating Point Numbers support scientific notation, with at least one digit after the decimal point.

The numeric type inheritance diagram is as follows:
Copy codeThe Code is as follows:
Numeric
| -- Integer
| -- Fixnum
| -- Bignum
| -- Float
| -- Complex (Standard Library)
| -- BigDecimal (Standard Library)
| -- Rational (Standard Library)

Ii. String

Ruby contains no characters and only strings.

Single quotes character string, only 'and \ need to be escaped, other characters keep the literal meaning. ('Indicates the beginning and end of a single quotation mark string, so it must be escaped. If you only have one character \ in a single quotation mark string, you must understand it .)

Double quotation mark string. The biggest feature of a double quotation mark string is that it can be used for numerical interpolation. There are many ways to generate double quotation mark strings.

Copy codeThe Code is as follows:
Inserts = 100
# Single quotation mark string
Print '# {insert} _ string' # {insert} _ string
Print "\ n"
Print % q/# {insert }_string/# separator % q indicates a single quotation mark string
Print "\ n"

# Double quotation mark string
Print "# {insert} _ string \ n" # 100_string
Print %/# {insert} _ string \ n/# separator % or % Q can represent double quotation mark strings
Print % Q/# {insert }_string \ n/

# Multiline string
Print "first line
Second line
Third line \ n "# multiple lines can be written directly

Print <-'multi _ line' #-This removes the need to start a line. ''indicates that no interpolation is performed.
First line # {insert}
Second line
Multi_line

Str1 = 'good' # The string can be modified.
Str1 [0] = 'H'
Print str1

Iii. Interval

Interval provides a simple way to process object sets with continuous values. To save space, ruby only retains references of the first and last objects in the interval in the memory.

Copy codeThe Code is as follows:
For I in 1 .. 3 # closed interval, 123 output
Print I
End

Print "\ n"

For I in "num1"... "num3" # first closed and then open, output num1num2
Print I
End

Iv. Array

A collection of various types of objects.
Copy codeThe Code is 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 type
Print arr2 [1]. class # String type

5. Hash

Key-value pair set, widely used
Copy codeThe Code is as follows:
Hash1 = {1 => "first", "second" => 2}
Print hash1 ["second"]

Vi. Symbols

Because the same string has different copies in the memory, the symbol type is used to save memory. The same symbol only has one copy in the memory, note that strings and symbols are of different types.
Copy codeThe Code is as follows:
Print "string". object_id, "\ n" # the same string has different IDs.
Print "string". object_id, "\ n"
Print: string. object_id, "\ n" # the same symbol has the same 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.