Ruby Data Types
In this chapter we will introduce the basic data types of Ruby.
The data types supported by Ruby include the basic Number, String, Ranges, Symbols, as well as the special values true, false and nil, and there are also two important data structures Array and Hash.
Numeric Types (Number)
1. Integer
Integers are of two kinds: if within 31 bits (four bytes), it is a Fixnum instance; if it exceeds that, it is a Bignum instance.
Integers range from -230 to 230-1; integers in this range are objects of the class Fixnum , and when an integer value is greater than or equal to 2 to the 30th power ( -262 to 262-1), it is automatically converted to the Bignum type.
You can use an optional leading sign before an integer, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a string of digits. Underscore characters are ignored in numeric strings.
You can obtain the integer value of an ASCII character or an escape sequence marked with a question mark.
Example
123
Fixnum decimal
1
_234
Fixnum decimal with underscores
500
Negative Fixnum
0377
Octal
0xff
Hexadecimal
0
b1011
Binary
"
a
"
.
ord
Character code of "a"
?\
n
Encoding of the newline character (0x0a)
12345678901234567890
Large numbers
Integer: Here are some integer literals
Literal: Values that can be seen in code, such as numbers, boolean values, and strings, are all called literals.
Such as the following 0, 1_000_000, 0xa, etc.
a1
=
0
Integers with thousands separators
a2
=
1
_000_000
Representation in other bases
a3
=
0xa
puts
a1
,
a2
puts
a3
puts and print both print characters to the console, where puts includes a carriage return and line feed.
=
begin
This is a comment, called an embedded document comment, similar to
C
the /**/ in
=
end
Floating-point
Ruby supports floating-point numbers. They are numbers with decimals. A floating-point number is an object of the class Float , and can be any of the following.
Example
Example
123.4
Floating-point values
1.0e6
Scientific notation
4E20
Not required
4e+20
Sign before the exponent
Floating-point
f1
=
0.0
f2
=
2.1
f3
=
1000000.1
puts
f3
Arithmetic Operations
Addition, subtraction, multiplication and division operators: + - * /; the exponentiation operator is **
The exponent does not have to be an integer, for example
Example
Exponent arithmetic
puts
2
**
(
1
/
4) # The quotient of 1 and 4 is 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 take the fourth root
String type
Simply put, a Ruby string is a sequence of 8-bit bytes, and they are objects of the class String.
Strings marked with double quotes allow interpolation and the use of backslash symbols, while strings marked with single quotes do not allow interpolation and only allow the two backslash symbols \ and '.
Example
!/usr/bin/ruby -w
puts
'
escape using "
\
"
'
;
puts
'
That
'
s right
'
;
Try it »
This will produce the following result:
escape using "\"
That's right
You can use the sequence #{ expr } to replace the value of any Ruby expression with a string. Here, expr can be any Ruby expression.
Example
!/usr/bin/ruby -w
puts
"
Multiplication: #{246060}
"
;
This will produce the following result:
相乘 : 86400
Example
!/usr/bin/ruby -w
name
=
"
Ruby
"
puts
name
puts
"
#{name+
"
,
ok
"
}
"
The output is:
Ruby
Ruby,ok
Backslash Symbols
The following table lists the backslash symbols supported by Ruby:
| sign before positive numbers | Represented character |
|---|---|
| \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, see Ruby Strings (String)。
arrays
Array literals are defined with comma-separated values inside [], and range definitions are supported.
- (1) Arrays are accessed by index via []
- (2) Insert, delete, and replace elements through assignment operations
- (3) Combine and delete elements through the + and - operators, and the collection appears as a new collection
- (4) Through the<
- (5) Repeat array elements through the * operator
- (6) Perform union and intersection operations through the | and & operators (note the order)
Example
!/usr/bin/ruby
ary
=
[
"
fred
"
,
10
,
3.14
,
"
This is a string
"
,
"
last element
"
,
]
ary
.
each
do
|
i
|
puts
i
end
Try it »
This will produce the following result:
fred
10
3.14
This is a string
last element
For more details about Ruby arrays, see Ruby Arrays (Array)。
Hash Type
A Ruby hash places a series of key/value pairs inside braces, with keys and values separated by commas and the sequence =>. A trailing comma is ignored.
Example
Example
!/usr/bin/ruby
hsh
=
colors
= {
"
red
"
=>
0xf00
,
"
green
"
=>
0x0f0
,
"
blue
"
=>
0x00f
}
hsh
.
each
do
|
key
,
value
|
key
,
"
is
"
,
value
,
"
\n
"
end
Try it »
This will produce the following result:
red is 3840
green is 240
blue is 15
For more details about Ruby hashes, see Ruby Hashes (Hash)。
Range Types
A range represents an interval.
A range is represented by setting a start value and an end value. A range can be constructed using s..e and s...e, or through 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, a 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.
Example
!/usr/bin/ruby
(
.15
)
.
each
do
|
n
|
n
,
'
'
end
Try it »
This will produce the following result:
10 11 12 13 14 15
For more details about Ruby ranges, see Ruby Ranges (Range)。