Latest web development tutorials

Ruby data type

This chapter we will introduce Ruby basic data types.

Ruby Supported data types include basic Number, String, Ranges, Symbols, and true, false, and nil these special values, as well as two important data structures --Array and Hash.


Value Type (Number)

1, Integer (Integer)

Integer two, if 31 or less (four bytes), it is Fixnum instance. If so, is Bignum instance.

Integer range from -2 or -2 30-2 30-1 62-2 62-1. Integer in this range is the classFixnumobject integer stored outside the scope of this classBignumobjects.

You can use an optional preamble symbol in front of an integer, an optional base indicator (0 for octal, 0x correspond hex, 0b corresponding to binary), followed by a string of numbers. Underscore character numeric string are ignored.

You can get an ASCII character or an integer value escape sequence with question marks.

Examples

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 

Float

Ruby support floating point. They are numbers with decimals. FloatFloatis a class of objects, and can be any one of the following.

Examples

123.4                # 浮点值
1.0e6                # 科学记数法
4E20                 # 不是必需的
4e+20                # 指数前的符号
#浮点型 
f1=0.0 
f2=2.1 
f3=1000000.1 
puts f3  

Arithmetic operation

Arithmetic operators: + - * /; operator to index **

Index need not be an integer, e.g.

#指数算术 
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 is the class String object.

String of double quotation marks and backslashes allow replacement strings are not allowed to replace the single quotation marks, and only allows the use of \\ and \ 'two backslash.

Examples

Examples

#! / Usr / bin / ruby ​​-w

puts 'escape using "\\"';
puts' That \ 's right';

Running instance »

This produces the following results:

escape using "\"
That's right

You can sequence# {expr} is replaced with a string of arbitrary Ruby expressions use.Here, expr can be any Ruby expression.

#! / Usr / bin / ruby ​​-w

puts "Multiplication Value: # {24 * 60 * 60}";

This produces the following results:

Multiplication Value: 86400
#!/usr/bin/ruby -w

name="Ruby" 
puts name 
puts "#{name+",ok"}" 

The output is:

Ruby
Ruby,ok

Backslash

The following table lists the supported Ruby backslash:

符号表示的字符
\n换行符 (0x0a)
\r回车符 (0x0d)
\f换页符 (0x0c)
\b退格键 (0x08)
\a报警符 Bell (0x07)
\e转义符 (0x1b)
\s空格符 (0x20)
\nnn八进制表示法 (n 是 0-7)
\xnn十六进制表示法 (n 是 0-9、a-f 或 A-F)
\cx, \C-xControl-x
\M-xMeta-x (c | 0x80)
\M-\C-xMeta-Control-x
\x字符 x

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

Array

Array literal by [] to define a comma-separated, and support range definitions.

  • (1) through the array [] index access
  • (2) by assignment to insert, delete, replace element
  • (3) + - No. merge and delete elements, and the collection as a new collection appears
  • (4) by additional data elements the original resolution <<
  • (5) by repeating array element number *
  • (6) | and the ampersand do union and intersection operations (note the order)

Example:

Examples

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

Running instance »

This produces the following results:

fred
10
3.14
This is a string
last element

For more details about the Ruby array, see the Ruby Array (the Array) .

Hash Type

Ruby hash is placed in braces series of key / value pairs, use commas between the keys and values ​​and sequence => separation. Trailing comma is ignored.

Examples

Examples

#! / Usr / bin / ruby

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

Running instance »

This produces the following results:

red is 3840
green is 240
blue is 15

For more details about the Ruby hash, see the Ruby hash (the Hash) .

Range Type

A range represents a range.

Range is a start value and an end value by setting to represent. The range of usable s..e and s ... e to construct, or by Range.new constructed.

Constructed using .. run from the range starting value to end value (including the end values). ... Constructed using a range run from start value to end value (end value is not included). When used as an iterator to use the range will return each value in the sequence.

Range (1..5) means that it contains a value of 1, 2, 3, 4, 5, range (1 ... 5) means that it contains the value 1, 2, 3, 4.

Examples

Examples

#! / Usr / bin / ruby

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

Running instance »

This produces the following results:

10 11 12 13 14 15

For more details about the Ruby range, see the Ruby Range (the Range) .