Ruby data types
In this chapter 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 of true, false, and nil, as well as two important data structures-Array and Hash.
Numeric type (Number)
1. Integer type (Integer)
There are two types of integer types. If it is within 31 bits (four bytes), That is a Fixnum instance. If it exceeds, it is a Bignum instance.
The integer range is from -230 to 230-1 or -262 to 262-1 . Integers within this range are objects of class Fixnum, and integers outside this range are stored in objects of 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 the integer value of an ASCII character or an escape sequence marked with a question mark.
Example
123 # Fixnum 十进制 1_234 # Fixnum 带有下划线的十进制 -500 # 负的 Fixnum 0377 # 八进制 0xff # 十六进制 0b1011 # 二进制 "a".ord # "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
Floating point
Ruby supports floating point numbers. They are numbers with decimals. Floating point numbers are objects of class Float and can be any of the following.
Example
123.4 # 浮点值 1.0e6 # 科学记数法 4E20 # 不是必需的 4e+20 # 指数前的符号
#浮点型 f1=0.0 f2=2.1 f3=1000000.1 puts f3
Arithmetic operations
Addition, subtraction, multiplication and division operators: +-*/; the exponent operator is **
The exponent 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(四分之一),然后开四次方根
String type
Ruby string is simply a sequence of 8-bit bytes, which are objects of class String.
Double-quote-marked strings allow replacement and the use of backslash symbols. Single-quote-marked strings do not allow replacement, and only the two backslash symbols \\ and \' are allowed.
Instance
Instance
#!/usr/bin/ruby -w puts 'escape using "\"'; puts 'That\'s right';
Run Instance»
Click the "Run Instance" button to view online Example
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.
#!/usr/bin/ruby -w puts "Multiplication Value : #{24*60*60}";
This will produce the following result:
Multiplication Value : 86400
#!/usr/bin/ruby -w name="Ruby" puts name puts "#{name+",ok"}"
The output will be:
Ruby Ruby,ok
Backslash symbol
The following table lists the backslash supported by Ruby Slash symbol:
The character represented by the symbol | |
---|---|
\n | line break Character (0x0a) |
\r | Enter character (0x0d) |
\f | Replace Page break (0x0c) |
\b | Backspace key (0x08) |
\a | Alarm Bell (0x07) |
Escape character (0x1b) | |
Space character (0x20) | ##\nnn |
\xnn | |
##\cx, \C-x | |
\M-x | |
\M-\C-x | |
##\x | characters x |