ruby and watir学习之_Array 类

Array 类说明

函数名称

说明

示例

&

数组与,返回两数组的交集

[1,2] & [2,3]   » [2]

*

复制数组 n 次

[1,2]*2            » [1,2,1,2]

+

返回两数组的并集,但不排除重复元素

[1,2]+[2,3]      » [1,2,2,3]

<< 

追加元素,但不排除重复元素

[1,2]<<[2,3]     » [1,2,2,3]

|

追加元素,但排除重复元素

1,2] | [2,3]      » [1,2,3]

 

-

返回第一个数组与第二个数组不同的元

 

[1,2]-[2,3]      » [1]

<=>

比较数组

[1,2]<=>[2,3]   »flase

==

比较数组,若所有元素均相等时返回真

[1,2]==[2,1]      »flase

assoc

从数组的每个元素中寻找指定对象

[[1,2],[3,4]].assoc(2)  » [1,2]

 

at

找到数组的第 N 个元素

负数表示逆向查找

["a","b","c","d","e"].at(0) » "a" ["a","b","c","d","e"].at(-1)   » "e"

clear

删除数组中的所有元素

["a","b","c","d","e"]. clear

collect collect

用一个过程块对数组的每个元素进行处

["a","b","c","d"].collect {|x| x + "!" }

» ["a!", "b!", "c!", "d!"]

 

compact compact!

 

删除值为 nil 的元素后生成新数组并返 回它

["a",nil,"b",nil,"c",nil].compact

» ["a", "b", "c"]

 

 

delete

 

删除元素,如果元素重复,全部删除

a = [ "a", "b", "b", "b", "c" ]

a.delete("b")

puts a       » ["a","c"]

 

 

 

delete_at

 

删除 pos 所指位置的元素并返回它。若

pos 超出数组范围则返回 nil

a = %w( ant bat cat dog )

a.delete_at(2)    » "cat"

a                          » ["ant", "bat", "dog"]

a.delete_at(99)    » nil

 

delete_if

根据条件删除

a = [ "a", "b", "c" ]

a.delete_if {|x| x >= "b" }   » ["a"]

 

 

each

 

对数组的每个元素按值进行迭代操作

a = [ "a", "b", "c" ]

a.each {|x| print x, " -- " }

»    "a -- b -- c --"

 

 

each_index

 

对数组的每个元素按索引进行迭代操作

a = [ "a", "b", "c" ]

a.each_index {|x| print x, " -- " }

» "0 -- 1 -- 2 --"

empty?

判断数组是否为空,为空则返回真

[].empty?   » true

eql

比较两数组是否相等

["a","b","c"].eql?(["a","b","c"]) » true

 

 

fill

 

 

填充数组

["a","b","c","d"].fill("x")

» ["x","x","x","x"] ["a","b","c","d"].fill("z", 2, 2)

» ["x", "x", "z", "z"]

 

first

返回数组的首元素。若没有首元素则返

回 nil

 

[ "q", "r", "s", "t" ].first      » "q"

 

last

返回数组末尾的元素。若数组为空时,

返回 nil

 

["w","x","y","z"].last           » "z"

 

 

include?

 

判断数组中是否包含元素

a = [ "a", "b", "c" ]

a.include?("b")  » true a.include?("z")  » false

 

 

index

 

返回数组中第一个==val 的元素的位置

a = [ "a", "b", "c" ]

a.index("b")  » 1 a.index("z")   » nil

 

 

indexes

 

以数组形式返回其索引值与各参数值相 等的元素

a = [ "a", "b", "c", "d", "e", "f", "g" ]

a.indexes(0, 2, 4)         » ["a", "c", "e"]

a.indexes( 2, 4, 12)      » [ "c", "e", nil]

 

 

insert

 

在索引为 nth 的元素前面插入第 2 参数 以后的值

ary = %w(foo bar baz)

ary.insert 2,'a','b'

p ary     » ["foo", "bar", "a", "b", "baz"]

 

join

将数组元素按一定的分隔符连接起来

[ "a", "b", "c" ].join  » "abc"

[ "a", "b", "c" ].join("-")  » "a-b-c"

length

size

返回数组长度。若数组为空则返回 0

[1,2,3].length        » 3 [1,2,3].size            » 3

nitems

返回非 nil 元素的个数

[ 1, nil, 3, nil, 5 ].nitems  » 3

 

 

pop

 

删除末尾元素并返回它。若数组为空则 返回 nil

a = [ "a", "m", "z" ]

a.pop                            » "z"

p a                               » ["a", "m"]

 

push

添加新元素

["a","b"].push(['1','2'])

» ["a", "b", ["1", "2"]]

 

 

rassoc

遍历数组每个元素(元素必须是数组),

匹配索引为 1 的值是否与查找的字符相 等,返回第一个相等的元素

 

a = [[15,1], [25,2], [35,2]]

p a.rassoc(2)                  » [25, 2]

 

 

replace

 

替换数组元素

a = ["a","b"]

a.replace(["x","y","z"])

p a                                   »["x", "y", "z"]

reverse reverse

将所有元素以逆序重新排列生成新数组

并返回它

 

["a","b","c" ].reverse  » ["c", "b", "a"]

 

rindex

返回最后一个值相等的元素的索引值

a = [ "a","b","b","b","c"]

a.rindex("b")                        » 3

 

 

shift

 

删除数组的首元素并返回它。剩余元素 依次提前。若数组为空返回 nil。

args = ["-m","-q","filename"]

args.shift   » "-m"

args   » ["-q", "filename"]

sort

sort!

从小到大排序

a = [ "d", "a", "e", "c", "b" ]

a.sort       » ["a", "b", "c", "d", "e"]

uniq uniq

删除数组中的重复元素后生成新数组并

返回它

a = [ "a", "a", "b", "b", "c" ]

a.uniq            » ["a", "b", "c"]

 

unshift

在数组第一个元素前添加元素

a = [ "b", "c", "d" ]

a.unshift("a")      » ["a", "b", "c", "d"]

to_s

将数组的所有元素连接成字符串

["a","e","i","o"].to_s         » "aeio"

posted on 2010-07-28 13:20  张飞_  阅读(992)  评论(0编辑  收藏  举报

导航