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:

##\eEscape character (0x1b)##\s##\nnnOctal notation (n is 0-7)\xnnHexadecimal notation (n is 0-9, a-f or A-F)Control-xMeta-x (c | 0x80)Meta- Control-x##\x characters x

For more details about Ruby strings, see Ruby Strings (String).

Array

Array literals are defined by commas in [] and support range definitions.

  • (1) Array access through [] index

  • (2) Inserting, deleting, and replacing elements through assignment operations

  • (3) Use the + and - signs to merge and delete elements, and the set will appear as a new set

  • (4) Use the << sign to merge and delete elements. Data append element

  • (5) Repeat array element

  • through * symbol (6) Perform union and intersection operations through | and & symbols (Note the order)

Instance:

Instance

#!/usr/bin/ruby
ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
    puts i
end

Run instance»

Click the "Run Instance" button to view the online instance

This will produce the following results:

fred
10
3.14
This is a string
last element

For more details about Ruby arrays, check out Ruby Arrays ( Array).

Hash type

Ruby hashing is a series of key/value pairs placed within curly braces, with the keys and values ​​separated by commas and the sequence =>. Trailing commas are ignored.

Instance

Instance

#!/usr/bin/ruby

hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
    print key, " is ", value, "\n"
end

Run Instance»

Click the "Run Instance" button to view online Example

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 (Hash).

Range type

A range represents an interval.

The range is represented by setting a start value and an end value. Ranges can be constructed using s..e and s...e, or via Range.new.


A range constructed using .. runs from the start value to the end value (inclusive). A range constructed with ... runs from the start value to the end value (exclusively). When used as an iterator, a range returns each value in the sequence.

Range (1..5) means it contains the values ​​1, 2, 3, 4, 5, range (1...5) means it contains the values ​​1, 2, 3, 4.

Instance

Instance

#!/usr/bin/ruby

(10..15).each do |n|
    print n, ' '
end

Run Instance»

Click the "Run Instance" button to view online Example

This will produce the following results:

10 11 12 13 14 15

For more details about Ruby ranges, see Ruby Ranges (Range).

The character represented by the symbol
\nline break Character (0x0a)
\rEnter character (0x0d)
\fReplace Page break (0x0c)
\bBackspace key (0x08)
\aAlarm Bell (0x07)
Space character (0x20)
##\cx, \C-x
\M-x
\M-\C-x