工作中常常要处理Array,索性把ruby 中Array的一些常用方法小结一下,加深印象。
先来看看Array中除去继承自Object之外的,有多少public methods
(Array.instance_methods - Object.instance_methods).count
# 输出:
120
# ruby版本为 2.3.1
正文
建立空数组:
[] Array.new
使用%w, %i 创建非空数组
创建不包含空白的字符串数组,可用%w
%w(Ruby Python Scheme) # 输出: ["Ruby", "Python", "Scheme"]
创建元素为符号的数组,可用%i
%i(Ruby Python Scheme) # 输出: [:Ruby, :Python, :Scheme]
数组的大小
[4, 8, 15, 16, 23, 42].count [4, 8, 15, 16, 23, 42].size [4, 8, 15, 16, 23, 42].length # 输出: 6 6 6
将string转化为array
split
"a, b, c, d".split(", ") # 输出: ["a", "b", "c", "d"]
将array转化为string
join
["a", "b", "c", "d"].join(", ") # 输出: "a, b, c, d"
将hash 转化为array
to_a
table = {:"color1" => "blue", :"color2" => "red"} table.to_a # 输出: [[:color1, "blue"], [:color2, "red"]] table.to_a.flatten # 输出: [:color1, "blue", :color2, "red"]
在数组中提取元素
index从0开始,index为正时,从array的第一个元素开始,index为负时,从最后一个元素开始
[1, 2, 3, 4, 5][2] # 输出: 3 [1, 2, 3, 4, 5][-1] # 输出: 5
给数组添加元素
在尾部添加<< or push
[1, 2, 3, 4, 5] << "woot" # 输出: [1, 2, 3, 4, 5, "woot"] [1, 2, 3, 4, 5].push("woot") # 输出: [1, 2, 3, 4, 5, "woot"]
在开头添加 unshift
[1, 2, 3, 4, 5].unshift(0) # 输出: [0, 1, 2, 3, 4, 5]
连接数组concat / +
[1, 2, 3, 4, 5].concat([6, 7]) # 输出: [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5] + [6, 7] # 输出: [1, 2, 3, 4, 5, 6, 7]
数组转换map, collect
[1, 2, 3, 4, 5].map {|x| x * 3} # 输出: [3, 6, 9, 12, 15] [1, 2, 3, 4, 5].collect {|x| x + 2 } # 输出: [3, 4, 5, 6, 7]
获取分散的数组values_at
[1,2,3,4,5,6].values_at(1,2,4) # 输出: [2, 3, 5]
筛选特定的元素 select
[1,2,3,4,5,6].select {|number| number % 2 == 0} # 输出: [2, 4, 6]
删除元素 delete
a = [1,3,5,4,6,7] a.delete(5) # 注意这里delete的参数是元素,不是index # 输出: 5 a # 输出: [1, 3, 4, 6, 7]
删除指定index的元素delete_at 【破坏性修改】
a = [1,3,5,4,6,7] a.delete_at(5) # 输出: 7 a # 输出: [1,3,5,4,6]
删除符合要求的delete_if【破坏性修改】, reject
[1,2,3,4,5,6,7,8,9].delete_if{|x| x % 2 == 0} # 输出: [1, 3, 5, 7, 9] [1,2,3,4,5,6,7,8,9].reject{|x| x % 2 == 0} # 输出: [1, 3, 5, 7, 9]
删除所有nil元素compact
a = [nil, 4, nil, 8, 15, 16, nil, 23, 42, nil] a.compact # 输出: [4, 8, 15, 16, 23, 42]
删除指定部分slice
a = [1,3,5,4,6,7] a.slice(1,2) # 输出: [3, 5] a.slice!(1,2) # 输出: [3, 5] a # 输出: [1, 4, 6, 7]
删除第X个元素shift, 默认是第一个【破坏性修改】
a = [1,2,3,4,5,6,7,8,9] a.shift # 输出: 1 a # 输出: [2, 3, 4, 5, 6, 7, 8, 9] a.shift(2) # 输出: [2, 3] a # 输出: [ 4, 5, 6, 7, 8, 9]
删除最后X个元素pop ,默认是最后一个【破坏性修改】
a = [1,2,3,4,5,6,7,8,9] a.pop # 输出: 9 a # 输出: [1,2,3,4,5,6,7,8] a.pop(3) # 输出: [6,7,8] a # 输出: [1,2,3,4,5]
ruby中method后面带有!符号的都是具有破坏性的修改, 但是那些没有带!尾巴的也不是那么友好,比如上面的shift, pop。 那么可有办法保护我们的array?有滴,上freeze
freeze可以禁止具有破坏性的方法来修改对象。
a = [1,2,3,4,5,6,7,8,9] a.freeze a.pop # 输出: # can't modify frozen Array a # 输出: [1,2,3,4,5,6,7,8,9]
去除重复元素uniq
a = [1,3,5,4,6,7,1,3,7] a.uniq # 输出: [1, 3, 5, 4, 6, 7]
平坦化数组flatten
前面出现过,再多看些例子:
[4, [8], [15], [16, [23, 42]]].flatten # 输出: [4, 8, 15, 16, 23, 42]
不传递参数时,默认是完全平坦化,传递参数,则平坦化层级X,例如:
[4, [8], [15], [16, [23, [42, 43]]]].flatten(1) # 输出: [4, 8, 15, 16, [23, [42, 43]]] [4, [8], [15], [16, [23, [42, 43]]]].flatten(2) # 输出: [4, 8, 15, 16, 23, [42, 43]] [4, [8], [15], [16, [23, [42, 43]]]].flatten(3) # 输出: [4, 8, 15, 16, 23, 42, 43]
获取某个元素的出现次数count
count前面已经出现过,用于计算数组的长度,不带参数,如果传递参数给到count,则返回该元素在数组中出现的次数。
[42, 8, 15, 16, 23, 42].count(42) # 输出: 2
排序sort
a = [1, 3, 5, 4, 6, 7] a.sort # 输出: [1, 3, 4, 5, 6, 7]
使用块来指定排序规则:
a = ["ruby", "python", "C", "C++", "java"] a.sort # 输出: ["C", "C++", "java", "python", "ruby"] # 默认按照首字母排序 a.sort {|a, b| a.length <=> b.length} # 输出: ["C", "C++", "ruby", "java", "python"] # 按照字符长度来排序
同样的,sort!方法具有破坏性。
The End
先整这么多,用到新的再补上,嘿哈