SoFunction
Updated on 2025-04-07

Brief introduction to the basic data types of ruby

The basic data types in general and ruby ​​are all class types, but for the convenience of writing and reading, it provides a relatively concise way of writing for basic data types.

1. Numerical type

Integers support binary, octal, decimal, and hexadecimal. Dynamically determine whether the integer is Fixnum type or Bignum type based on the size of the integer.

Floating points support scientific notation, with at least one number after the decimal point.

The numerical type inheritance diagram is as follows:

Copy the codeThe code is as follows:

Numeric
 |--Integer
     |--Fixnum
         |--Bignum
 |--Float
|--Complex (standard library)
|--BigDecimal (standard library)
|--Rational (standard library)

2. Character strings

There are no characters in ruby, only strings.

Single quote strings, only ' and \ need to be escaped, and other characters keep the literal meaning. (' means the beginning and end of a single quote string, so it needs to be escaped. If you think about the single quote string, you will understand that \ also needs to be escaped.)

Double quoted strings. The biggest feature of double quoted strings is that they can interpolate numerical values, and there are many ways to generate double quoted strings.

Copy the codeThe code is as follows:

insert=100 
#Single quote string
print '#{insert}_string'    #{insert}_string 
print "\n" 
print %q/#{insert}_string/  #delimiter %q represents a single quote string
print "\n" 
 
#Double Quotation String
print "#{insert}_string\n"  #100_string 
print %/#{insert}_string\n/ #delimiter % or %Q can represent double quote strings
print %Q/#{insert}_string\n/ 
 
#Multi-line string
print "first line 
second line 
third line\n" #can be written directly on multiple lines
 
print <<-'multi_line' #-It can make the termination symbol not necessary to be at the beginning of the line, '' means no interpolation is performed.
first line#{insert} 
second line 
    multi_line 
 
str1='good' #The string can be modified
str1[0]='h' 
print str1 

3. Range

Intervals provide an easy way to process sets of objects with continuous characteristics of values. In order to save space, ruby ​​only retains references to the two objects at the beginning and end of the interval in memory.

Copy the codeThe code is as follows:

for i in 1..3 #Close interval, output 123
    print i 
end 
 
print "\n" 
 
for i in "num1"..."num3" #Open after first closing, output num1num2
    print i 
end 

IV. Array

Can accommodate a collection of objects of various types.

Copy the codeThe code is as follows:

arr1=[1,2,3,"num1"] 
arr2=%w/1 2 3 num1/ #%w and %W are character array separators, 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. Scatter list

A collection of key-value pairs, widely used

Copy the codeThe code is as follows:

hash1={1=>"first","second"=>2} 
print hash1["second"] 

Six. Symbols

Since the same string has different copies in memory, symbol types are used to save memory. The same symbols only have one copy in memory. In addition, it is necessary to note that strings and symbols are completely different types.

Copy the 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"