• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby中的数组相关方法介绍

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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

 

 

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

l2 = ["A","B","C"]

l3 = ["x","y","z"]

r = l1.zip(l2,l3)

r.each do |i|
  
  puts i
end

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
linux安装ruby发布时间:2022-07-14
下一篇:
为什么Ruby on Rails: 一张有趣的图片。发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap