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. 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. Ruby supports floating-point numbers. They are numbers with decimals. Floating point numbers are classes Addition, subtraction, multiplication and division operator: +- * /;The exponential operator is * * The index does not have to be an integer, for example The Ruby string is simply an 8-bit byte sequence, which is a class Double quoted strings allow substitution and the use of backslashes, while single quoted strings do not allow substitution, and only two backslashes, This will produce the following results: You can use sequences This will produce the following results: The output is as follows: 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 literals pass through (1)the array is passed (2)insert, delete and replace elements through assignment operations (3)pass (4)pass (5)pass (6)pass This will produce the following results: For more details about the Ruby array, see the Ruby array (Array). A Ruby hash places a series of key / value pairs within curly braces, using commas and sequences between keys and values. This will produce the following results: For more details about Ruby hashes, check out Ruby hashes. A range represents an interval. The range is represented by setting a start value and an end value. The range can be used Principles, Technologies, and Methods of Geographic Information Systems 102 In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress. 6.10.1. Numeric type (Number) #
1.Integer #
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 #
Float
and can be any of the followingExample #
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 #
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 #
String
object.
\\\
and
`\\'
, are allowed.Example #
#!/usr/bin/ruby -wputs'escape using "\\\\"';puts'That\\'s right';
escape using "\"
That's right
#{
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}";
multiply : 86400
Example #
#!/usr/bin/ruby -wname="Ruby"putsnameputs"#{name+",ok"}"
Ruby
Ruby,ok
Backslash symbol #
Array #
[]
the definitions are separated by commas in, and range definitions are supported.
[]
index access
+
the-sign merges and deletes elements, and the collection appears as a new collection
<<
to append elements to the original data
*
number repeating array element
|
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
fred
10
3.14
This is a string
last element
Hash type #
=>
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
red is 3840
green is 240
blue is 15
Range Typ #
s..e
and
s...e
to construct, or through
Range.new
to construct.