2015-01-16 82 views
0

的日期我有一個數組,像這樣刪除數組中的元素:如何基於紅寶石

[{"day"=>"2014-04-08", "v"=>3}, 
{"day"=>"2014-04-09", "v"=>49}, 
{"day"=>"2014-04-10", "v"=>4}, 
{"day"=>"2014-04-11", "v"=>1587}, 
{"day"=>"2014-04-12", "v"=>20}, 

我如何刪除此數組中的所有元素,其中「天」是以後「2014- 04-10' 例如(只剩4/11和4/12)

回答

1
timestamp = Time.parse('2014-04-10') 
arr = [...] 

arr.keep_if { |h| Time.parse(h['day']) > timestamp } 
# => [{"day"=>"2014-04-11", "v"=>1587}, {"day"=>"2014-04-12", "v"=>20}] 
1

使用陣列#選擇或陣列#拒絕和比較‘天’使用Date類。

require 'date' 

[ 
    {"day"=>"2014-04-08", "v"=>3}, 
    {"day"=>"2014-04-09", "v"=>49}, 
    {"day"=>"2014-04-10", "v"=>4}, 
    {"day"=>"2014-04-11", "v"=>1587}, 
    {"day"=>"2014-04-12", "v"=>20} 
].select { |d| Date.parse(d["day"]) > Date.new(2014, 4, 10) }