partition.with_index

来源:互联网 发布:吉诺比利奇葩数据 编辑:程序博客网 时间:2024/05/23 16:49

I have an array filled with several dates

 arry = ["06-01-2014", "07-04-2014", "14-01-2014"         "14-04-2014", "16-01-2014" "27-03-2014",         "30-12-2013", "31-03-2014", "27-02-2014"]

I would like to remove element 6 and up Preferably have 2 different arrays.

Is that possible?

arry = ["06-01-2014", "07-04-2014", "14-01-2014","14-04-2014", "16-01-2014", "27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]first_five = arry.shift(5)#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014"]arry #=> ["27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]
arry = ["06-01-2014", "07-04-2014", "14-01-2014","14-04-2014", "16-01-2014", "27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]first_five,rest = arry.partition.with_index{|a,i| i < 5}first_five#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014"]rest#=> ["27-03-2014","30-12-2013", "31-03-2014", "27-02-2014"]arry#=> ["06-01-2014", "07-04-2014", "14-01-2014", "14-04-2014", "16-01-2014","27-03-2014", "30-12-2013", "31-03-2014", "27-02-2014"]

0 0
原创粉丝点击