码迷,mamicode.com
首页 > 编程语言 > 详细

ruby中的数组相关方法介绍

时间:2019-08-19 17:14:24      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:unshift   索引   赋值   大于   执行   ever   翻转   集合   效果   

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


puts l.values_at(2,3,4,6)
# 获取index为2、3、4、6的元素
#
#
l1 = ["a","b","c","d","e","f",‘g‘]
l2 =  ["a","b","c","d",1,2,3]


l = l1 & l2
# 取交集

puts l

l = l1 | l2
# 取并集
puts l

l = l1 - l2
# 获取在l1中,但是不是在l2中的元素

puts l


l = l1 + l2
# 获取l1和l2两个集合中的合集,这个是两个集合的简单相加,会有重复元素
puts l


puts "=" * 100
l = ["a","b","c","d",1,2,3]

l.unshift("0")
# 在数组头部追加一个元素
puts l


puts "=" * 100
l = ["a","b","c","d",1,2,3]
l.push(4)
# 在数组尾部追加一个元素
#
#
l<< 5
# 在数组尾部追加一个元素
puts l

puts "=" * 100
l = ["a","b","c","d",1,2,3]

a = l.shift
# 从头部删除一个元素,同时可以接受到删除的元素
puts l
puts a


puts "=" * 100
l = ["a","b","c","d",1,2,4]

a = l.pop
# 从数组的尾部删除一个元素,同时可以接受到删除的的元素

puts l

puts a



puts "=" * 100
l = ["a","b","c","d",1,2,4]

puts l.first
# 获取头部第一个元素
puts l.last
# 获取尾部第一个元素
#
#
#
#
puts "=" * 100
l1 = ["a","b","c","d","e"]
l2 = [1,2,3,"a"]

l1.concat(l2)
# 连接l1和l2,把l2追加到l1的后面,这个操作会重新赋值l1

puts l1


puts "=" * 100
l = [1,2,3,4,1,"a","b"]

l.delete(1)
# 删除数组中的指定元素
puts l



puts "=" * 100
l = [1,2,3,4,1,"a","b"]

l.delete_at(2)
# 删除数组中索引为2的元素
puts l


puts "=" * 100
l = [1,2,3,4,1,6,7]

l.delete_if {|i| i > 2 }
# 删除数组中大于2的元素

puts l


puts "=" * 100
l = [1,2,3,4,1,6,7]

l = l.reject {|i| i > 2 }
# 删除数组中大于2的元素,把删除后的元素赋值给一个新的数组,原来的数组不影响
puts l


puts "=" * 100
l = [1,2,3,4,1,6,7]

l.reject! {|i| i > 2 }
# 删除数组中大于2的元素,把删除后的元素赋值给原数组
puts l

puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.slice(1,3)
# 删除索引为1到3的元素,并把删除的元素赋值给新的数组,对原数组无影响
puts m



puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.slice!(1,3)
# 删除索引为1到3的元素,并把删除的元素赋值给新的数组,对原数组有影响
puts l



puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.uniq
# 移除数组中的重复元素,并把移除重复元素后的数组赋值给一个新的数组,对原数组无影响
# puts m
puts l



puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.uniq!
# 移除数组中的重复元素,并把移除重复元素后的数组赋值给一个新的数组,对原数组有影响
puts m
puts l


puts "=" * 100
l = [1,2,3,4,1,6,7]


m =  l.reverse
# 翻转数组,对原数组无影响,翻转后的数组赋值给新的数组
puts l
puts m

puts "=" * 100
l = [1,2,3,4,1,6,7]


m =  l.reverse!
# 翻转数组,对原数组有影响,翻转后的数组赋值给新的数组
puts m
puts l


puts "=" * 100
l = [1,2,3,4,1,6,7]

m =  l.collect {|item| item * 2 }

# 对原数组中的每个元素执行块中的运算,把计算后的元素赋值给一个新的元素,对原数组无影响

puts "=" * 100
l = [1,2,3,4,1,6,7]

m =  l.collect! {|item| item * 2 }
# 对原数组中的每个元素执行块中的运算,把计算后的元素赋值给一个新的元素,对原数组有影响
#

puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.map {|item| item + 100 }

# 效果同collect

puts "=" * 100
l = [1,2,3,4,1,6,7]

m = l.map! {|item| item + 100 }

# 效果同collect!
puts "=" * 100
l = [1,2,3,4,1,6,7]

l.fill(2)
# 替换数组中的所有元素为2
puts l


puts "=" * 100
l = [1,2,"a","a",1,6,7]

l.fill(3,3)
# 替换数组索引从3开始都尾部的所有的元素为3

puts l


puts "=" * 100
l = [1,2,"a","a",1,6,7]

l.fill(100,3,6)
# 替换数组索引从3开始到6结束的所有的元素为3
puts l


puts "=" * 100
l = [1,2,"a","a",[1,1,2,["c","c","d"]]]
l.flatten
# 展开嵌套的数组,对原数组无影响,展开后的数组会赋值给一个新的数组

puts "=" * 100
l = [1,2,"a","a",[1,1,2,["c","c","d"]]]
l.flatten!
# 展开嵌套的数组,对原数组有影响
#
#
#

puts "=" * 100
l = [1,2,0,100,1,6,7]

m = l.sort
# 对数组进行排序,不影响原数组
puts m

puts "=" * 100
l = [1,2,0,100,1,6,7]

m = l.sort!
# 对数组进行排序,影响原数组
puts m


puts "=" * 100
l = [1,2,0,100,1,6,7]

m = l.sort_by{|i| -i}
# 对数组进行指定方法的排序,对原数组无影响
puts m


puts "=" * 100
l = [1,2,0,100,1,6,7]

m = l.sort_by!{|i| -i}
# 对数组进行指定方法的排序,对原数组影响
puts m

 

ruby中的数组相关方法介绍

标签:unshift   索引   赋值   大于   执行   ever   翻转   集合   效果   

原文地址:https://www.cnblogs.com/bainianminguo/p/11378169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!