Ruby Data Types
In this chapter, we will introduce Ruby's basic data types.
The data types supported by Ruby include the basic Number, String, Ranges, Symbols, as well as the special values true, false, and nil, and two important data structures - Array and Hash.
Numeric Types (Number)
1. Integer
There are two types of integers. If within 31 bits (four bytes), it is a Fixnum instance. If it exceeds that, it is a Bignum instance.
The integer range is from -230to 230-1, and integers in this range are of classFixnumobjects. When the integer value is greater than or equal to 2 to the 30th power ( -262to 262-1), it will automatically be converted to Bignum type.
You can use an optional leading sign before the integer, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a series of digits. Underscore characters are ignored in the numeric string.
You can get the integer value of an ASCII character or an escape sequence marked with a question mark.
Example
Floating-point Type
Ruby supports floating-point numbers. They are numbers with decimal fractions. Floating-point numbers are of classFloatobjects, and can be any of the following.
Example
Example
Arithmetic Operations
Addition, subtraction, multiplication, and division operators: + - * /; exponent operator is **
The exponent need not be an integer, for example
Example
String Type
A Ruby string is simply a sequence of 8-bit bytes, and they are objects of the String class.
Strings delimited by double quotes allow substitution and the use of backslash notations; strings delimited by single quotes do not allow substitution, and only allow two backslash notations: \\ and \'.
This will produce the following result:
escape using "\" That's right
You can use the sequence#{ expr }to substitute the value of any Ruby expression into a string. Here, expr can be any Ruby expression.
Example
This will produce the following result:
相乘 : 86400
Example
The output result is:
Ruby Ruby,ok
Backslash Notations
The following table lists the backslash notations supported by Ruby:
| Notations | Character represented |
|---|---|
| \n | Newline (0x0a) |
| \r | Carriage return (0x0d) |
| \f | Form feed (0x0c) |
| \b | Backspace (0x08) |
| \a | Bell (0x07) |
| \e | Escape (0x1b) |
| \s | Space (0x20) |
| \nnn | Octal notation (n is 0-7) |
| \xnn | Hexadecimal notation (n is 0-9, a-f, or A-F) |
| \cx, \C-x | Control-x |
| \M-x | Meta-x (c | 0x80) |
| \M-\C-x | Meta-Control-x |
| \x | Character x |
For more details about Ruby strings, please seeRuby String。
Arrays
Array literals are defined by comma-separated values within [], and also support range definitions.
- (1) Arrays are accessed by index via []
- (2) Insert, delete, and replace elements through assignment operations
- (3) Use + and - to merge and remove elements, and the collection is returned as a new collection.
- (4) The << sign appends elements to the original data.
- (5) The * sign repeats array elements.
- (6) The | and & symbols are used for union and intersection operations (pay attention to the order).
Example
Try it »
This will produce the following result:
fred 10 3.14 This is a string last element
For more details about Ruby arrays, please seeRuby Array。
Hash Type
A Ruby hash is a series of key/value pairs placed inside braces, with keys and values separated by commas and the sequence =>. Trailing commas are ignored.
Example
Example
Try it »
This will produce the following result:
red is 3840 green is 240 blue is 15
For more details about Ruby hashes, please seeRuby Hash。
Range Type
A range represents an interval.
A range is represented by setting a start value and an end value. Ranges can be constructed using s..e and s...e, or via Range.new.A range constructed with .. runs from the start value to the end value (inclusive). A range constructed with ... runs from the start value to the end value (exclusive). When used as an iterator, the range returns each value in the sequence.
The range (1..5) means it contains the values 1, 2, 3, 4, 5, and the range (1...5) means it contains the values 1, 2, 3, 4.
This will produce the following result:
10 11 12 13 14 15
For more details about Ruby ranges, please seeRuby Range。
Other Extensions