May 12, 2021 Ruby
In this section, we'll introduce you to Ruby's basic data types.
Ruby supports data types such as basic Number, String, Ranges, Symbols, and special values such as true, false, and nil, as well as two important data structures, Array and Hash.
The integer is divided into two types, if within 31 bits (four bytes), that is the Fixnum instance. I f exceeded, it is a Bigum instance.
Integers range from -2 30 to 2 30-1 or -2 62 to 2 62-1. An integer in this range is an object of the class Fixnum, and an integer outside this range is stored in an object of the class Bignum.
You can use an optional leading symbol before the integer, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a string of numbers. Underscore characters are ignored in numeric strings.
You can get an ASCII character or an integer value for an escape sequence marked with a question mark.
123 # Fixnum 十进制 1_234 # Fixnum 带有下划线的十进制 -500 # 负的 Fixnum 0377 # 八进制 0xff # 十六进制 0b1011 # 二进制 ?a # 'a' 的字符编码 ?\n # 换行符(0x0a)的编码 12345678901234567890 # Bignum
#整型 Integer 以下是一些整型字面量 #字面量(literal):代码中能见到的值,数值,bool值,字符串等都叫字面量 #如以下的0,1_000_000,0xa等 a1=0 #带千分符的整型 a2=1_000_000 #其它进制的表示 a3=0xa puts a1,a2 puts a3 #puts print 都是向控制台打印字符,其中puts带回车换行符 =begin 这是注释,称作:嵌入式文档注释 类似C#中的/**/ =end
Ruby supports floating points. T hey are numbers with small numbers. Floating points are objects of the class Float and can be any of the following.
123.4 # 浮点值 1.0e6 # 科学记数法 4E20 # 不是必需的 4e+20 # 指数前的符号
#浮点型 f1=0.0 f2=2.1 f3=1000000.1 puts f3
Add and subtract the multiplication and divide the operator:
The index does not have to be an integer, for example
#指数算术 puts 2**(1/4)#1与4的商为0,然后2的0次方为1 puts 16**(1/4.0)#1与4.0的商为0.25(四分之一),然后开四次方根
The Ruby string is simply an 8-bit byte sequence, and they are objects of the class String.
Strings marked with double quotes allow the replacement and use of backslash symbols, strings marked with single quotes are not allowed to be replaced, and only two backslash symbols are allowed.
#!/usr/bin/ruby -w puts 'escape using "\\"'; puts 'That\'s right';
This results in the following:
escape using "\" That's right
You can replace the value of any Ruby expression with a string using the sequence . Here, expr can be any Ruby expression.
#!/usr/bin/ruby -w puts "Multiplication Value : #{24*60*60}";
This results in the following:
Multiplication Value : 86400
#!/usr/bin/ruby -w name="Ruby" puts name puts "#{name+",ok"}"
The output is:
Ruby Ruby,ok
The following table lists the anti-slash symbols supported by Ruby:
symbol | Characteristical |
---|---|
\n | Removal (0x0a) |
\r | Enter (0x0D) |
\f | Change page (0x0c) |
Return key (0x08) | |
\a | Alarm Bell (0x07) |
\e | Essential (0x1b) |
\s | Space character (0x20) |
\nnn | Octa representation (N is 0-7) |
\xnn | Hexadecimal representation (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 |
To learn more about Ruby strings, check out Ruby Strings.
Array literally is defined separated by a comma in , and the range definition is supported.
This results in the following:
fred 10 3.14 This is a string last element
To learn more about Ruby arrays, check out Ruby Arrays.
The Ruby hash is a series of key/value pairs placed in braces, separated by commas and sequences. The comma at the tail is ignored.
This results in the following:
green is 240 red is 3840 blue is 15
For more details about the Ruby hash, check out the Ruby hash.
A range represents a range.
The range is represented by setting a start value and an end value. T he range can be used by s. e and s... e to construct, or by Range.new.
Use.. T he range of constructs runs from the start value to the end value, including the end value. U se... T he range of constructs runs from the start value to the end value (does not include the end value). When used as an iterator, the range returns each value in the sequence.
A range (1..5) means that it contains values 1, 2, 3, 4, 5, range (1...5), which means that it contains values 1, 2, 3, 4.
This results in the following:
10 11 12 13 14 15
For more details about the Ruby range, check out the Ruby range.