1

私は配列を持っています:

array=[
{"name"=>"Henry Jones ", "Date"=>"2013-04-09 15:09:02", "Duration"=>"0:00:00", "Call Type"=>"Outgoing"},
{"name"=>"Barry Smith ", "Date"=>"2013-04-08 13:51:03", "Duration"=>"0:00:48", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-04 19:30:19", "Duration"=>"0:00:49", "Call Type"=>"Outgoing"},
{"name"=>"Alistair Morgan ", "Date"=>"2013-04-05 13:14:07", "Duration"=>"0:00:03", "Call Type"=>"Outgoing"}, 
{"name"=>"Quentin Blah", "Date"=>"2013-04-05 13:19:44", "Duration"=>"0:04:28", "Call Type"=>"Missed"}, 
{"name"=>"Quentin Blah", "Date"=>"2013-04-05 13:25:19", "Duration"=>"0:09:45", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-05 14:35:29", "Duration"=>"0:00:24", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-05 15:54:53", "Duration"=>"0:00:00", "Call Type"=>"Missed"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-06 16:21:20", "Duration"=>"0:00:47", "Call Type"=>"Outgoing"}
] 

日付文字列 (例: "2013-04-06") の日付を group_by したいと思いarray_of_hashes.group_by {|d| d["Date"] }ます。

時間を除いて日付でグループ化するにはどうすればよいですか? ティア

4

2 に答える 2

3

あなたはほとんどそこにいます。日付と時刻はスペースで区切られているため、String#splitを使用して日付と時刻を分割し、Array#firstを使用して日付を取得できます。

array_of_hashes.group_by do |hash|
  hash['Date'].split.first
end

IRB を使用すると、これがどのように機能するかを確認できます。

$ irb
2.0.0p0 :001 > date = "2013-04-06 16:21:20"
 => "2013-04-06 16:21:20" 
2.0.0p0 :002 > date.split
 => ["2013-04-06", "16:21:20"] 
2.0.0p0 :003 > date.split.first
 => "2013-04-06" 
于 2013-04-26T21:23:38.010 に答える
2

の値Dateは単なる文字列です。幸いなことに、これは適切に動作する固定幅のソート可能な文字列です。最初の 10 文字が必要です。

array=[
{"name"=>"Henry Jones ", "Date"=>"2013-04-09 15:09:02", "Duration"=>"0:00:00", "Call Type"=>"Outgoing"},
{"name"=>"Barry Smith ", "Date"=>"2013-04-08 13:51:03", "Duration"=>"0:00:48", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-04 19:30:19", "Duration"=>"0:00:49", "Call Type"=>"Outgoing"},
{"name"=>"Alistair Morgan ", "Date"=>"2013-04-05 13:14:07", "Duration"=>"0:00:03", "Call Type"=>"Outgoing"}, 
{"name"=>"Quentin Blah", "Date"=>"2013-04-05 13:19:44", "Duration"=>"0:04:28", "Call Type"=>"Missed"}, 
{"name"=>"Quentin Blah", "Date"=>"2013-04-05 13:25:19", "Duration"=>"0:09:45", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-05 14:35:29", "Duration"=>"0:00:24", "Call Type"=>"Incoming"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-05 15:54:53", "Duration"=>"0:00:00", "Call Type"=>"Missed"}, 
{"name"=>"Henry Jones ", "Date"=>"2013-04-06 16:21:20", "Duration"=>"0:00:47", "Call Type"=>"Outgoing"}
] 

p array.group_by{|call| call["Date"][0..9]}
于 2013-04-26T21:41:19.410 に答える