Ruby data type
In this section, we will introduce you to the basic data types of Ruby.
The data types supported by Ruby include basic Number, String, Ranges, Symbols, and special values such as true, false, and nil, as well as two important data structures-Array and Hash.
Numeric type (Number)
1.Integer
There are two types of integers. If the integer is less than 31 bits (4 bytes), it is a Fixnum instance. If it exceeds, it is a Bignum instance.
Integers range from-2:sup: 30 to 2 30 -1, the integers in this range are the class Fixnum When the integer value is greater than or equalto the 30th power of 2 (- 2:sup: 62 to 2 62 -1), which is automatically converted to the Bignum type.
You can use an optional leading symbol before an integer, an optional underlying metric (0 corresponds to octal,0x corresponding to hex,0b corresponding to binary), followed by a string of numbers. 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
123 # Fixnum Decimal 1\_ 234 # Fixnum decimal with underline -500 # negative
Fixnum0377 # Octal 0xff # Hexadecimal 0b1011 # Binary "a". ord # "a"
Character encoding for\ \N # Encoding of line break (0x0a) 12345678901234567890 # Large number # Integer
Integer
The following are some integer literals: values, numerical values, bool values,
strings, etc. that can be seen in the code are called literals, such as 0,1_ 000_ 000,0xa,
a1=0 # Integer with thousands a2=1\_ 000_ 000 # Other decimal representations a3=0xaputsa1, a2putsa3 # puts
Print
All characters are printed to the console, where puts comes with carriage return and line feed=begin.
This is a comment called an embedded document comment
Similar to/* */=end in C #
Floating point type
Ruby supports floating-point numbers. They are numbers with decimals. Floating point numbers are classes Float
and can be any of the following
Example
Example
123.4 # Floating-point value 1.0e6 # Scientific notation 4E20 # Not required 4e+20 # Sign before
exponent # Floating-point f1=0.0f2=2.1f3=1000000.1putsf3
Arithmetic operation
Addition, subtraction, multiplication and division operator: +- */;The exponential operator is * *
The index does not have to be an integer, for example
Example
#Exponential arithmetic puts2 * * (1/4) The quotient of # 1 and 4 is 0,
and then the 0th power of 2 is 1 puts
16 * * (1/4.0) The quotient of # 1 and 4.0 is 0.25 (quarter),
and then the root of the fourth power is taken
String type
The Ruby string is simply an 8-bit byte sequence, which is a class String
object.
Double quoted strings allow substitution and the use of backslashes, while single quoted strings do not allow substitution, and only two backslashes, \\\
and `\\'
, are allowed.
Example
#!/usr/bin/ruby -wputs'escape using "\\\\"';puts'That\\'s right';
This will produce the following results:
escape using "\"
That's right
You can use sequences #{ expr }
replace the value of any Ruby expression with a string. Here, expr
can be any Ruby expression.
Example
#!/usr/bin/ruby -wputs" multiply : #{24*60*60}";
This will produce the following results:
multiply : 86400
Example
#!/usr/bin/ruby -wname="Ruby"putsnameputs"#{name+",ok"}"
The output is as follows:
Ruby
Ruby,ok
Backslash symbol
The following table lists the backslash symbols supported by Ruby:
Symbol |
The character represented |
---|---|
\n |
Newline character (0x0a) |
\r |
Carriage return (0x0d) |
\f |
Feed character (0x0c) |
\b |
Backspace key (0x08) |
\a |
Alarm Bell (0x07) |
\e |
Escape character (0x1b) |
\s |
Space character (0x20) |
\nnn |
Octal representation (n is 0-7) |
\xnn |
Hexadecimal representation (n is 0-9, amurf, or Amurf) |
\cx, \C-x |
Control-x |
\ Mmurx |
Meta-x (c| 0x80) |
\ M -Cmurx |
Meta-Control-x |
\x |
Character x |
For more details about the Ruby string, see the Ruby string.
Array
Array literals pass through []
the definitions are separated by commas in, and range definitions are supported.
(1)the array is passed
[]
index access(2)insert, delete and replace elements through assignment operations
(3)pass
+
the-sign merges and deletes elements, and the collection appears as a new collection(4)pass
<<
to append elements to the original data(5)pass
*
number repeating array element(6)pass
|
and&
symbols do union and intersection operations (pay attention to the order)
Example
#!/usr/bin/rubyary=["fred",10,3.14,"This is a string","last
element",]ary.eachdo\|i\|putsiend
This will produce the following results:
fred
10
3.14
This is a string
last element
For more details about the Ruby array, see the Ruby array (Array).
Hash type
A Ruby hash places a series of key / value pairs within curly braces, using commas and sequences between keys and values. =>
separate. The trailingcomma is ignored.
Example
Example
#!/usr/bin/rubyhsh=colors=
{"red"=>0xf00,"green"=>0x0f0,"blue"=>0x00f}hsh.eachdo\|key,value\|printkey,"is",value,"\\n"end
This will produce the following results:
red is 3840
green is 240
blue is 15
For more details about Ruby hashes, check out Ruby hashes.
Range Typ
A range represents an interval.
The range is represented by setting a start value and an end value. The range can be used s..e
and s...e
to construct, or through Range.new
to construct.