0

必要な配列を作成する方法を決定しようとして問題が発生しています。

次のような配列があります。

["2013-10-01","2013-10-02","2013-10-07"]

10-01 が日曜日で、10-07 が月曜日です。各日付をそれぞれの配列にダンプしたいのですが、日付が存在しない場合 (10-03)、火曜日の配列に空の要素を配置したいと考えています。

コード例。

date_array = ["2013-10-01","2013-10-02","2013-10-07"]
sunday_array = []
monday_array = []
tuesday_array = []
wednesday_array = []
thursday_array = []
friday_array = []
saturday_array = []
sunday_array = []

date_array.each do |date|
  if date.include? (Date.parse(Time.now.to_s) - 39).strftime("%Y-%m-%d")
    sunday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 38).strftime("%Y-%m-%d")
    monday_array << date
  elsif date.include? (Date.parse(Time.now.to_s) - 37).strftime("%Y-%m-%d")
    tuesday_array << date
  #etc, etc.
  end
end

これは機能しますが、必要な空の値は作成されません。最後に、各配列は、thursday_array100% 空の値であっても、まったく同じ数の要素を持つ必要があります。

4

2 に答える 2

1

このプログラムは、あなたが求めるようにします。スタッフの各メンバーに対して、false値の 7 つの要素の配列を作成し、配列の各要素に対応する曜日を にdetails設定しtrueます。最後に、働いていない日はまだ含まれていますfalse

日曜日と土曜日をTime#wday返すことに注意してください。したがって、staff の配列の各メンバーは日曜日から土曜日になります。06

出力からわかるように、サンプル データのすべてのスタッフは、Ramielle Ford金曜日に休みを取った人を除いて、月曜日から金曜日まで働いていました。

このモジュールは、データを適切にレイアウトするコマンドをpp証明するためだけに含まれています。ppあなたはそれを必要としません。

require 'date'
require 'pp'

table = eval(<<__END__JSON__)
{"success"=>true, "data"=>[
    {"id"=>133572, "memberId"=>"103001862-07", "firstName"=>"Anissa", "lastName"=>"Martin", "dateOfBirth"=>"2002-11-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>133573, "memberId"=>"103001862-08", "firstName"=>"Allyson", "lastName"=>"Martin", "dateOfBirth"=>"2005-02-16", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"08:19:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"08:15:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:40:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"08:26:00", "checkOutTime"=>"08:00:00"}]},
    {"id"=>135692, "memberId"=>"103008263-05", "firstName"=>"Emmya", "lastName"=>"Burrell", "dateOfBirth"=>"2003-01-17", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"16:40:00"}]},
    {"id"=>138343, "memberId"=>"103015748-03", "firstName"=>"Abigail", "lastName"=>"Young", "dateOfBirth"=>"2003-08-09", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:35:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:36:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:40:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:30:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:30:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:56:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:26:00"}]},
    {"id"=>139451, "memberId"=>"103016684-03", "firstName"=>"William", "lastName"=>"Brown", "dateOfBirth"=>"2004-02-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>1, "checkInTime"=>"07:00:00", "checkOutTime"=>"08:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8748758, "memberId"=>"2162539-05", "firstName"=>"Ramielle", "lastName"=>"Ford", "dateOfBirth"=>"2009-08-06", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}]},
    {"id"=>8795043, "memberId"=>"2163877-04", "firstName"=>"Christopher", "lastName"=>"Lampkins", "dateOfBirth"=>"2006-11-26", "details"=>[{"date"=>"2013-10-01T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-02T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-03T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-04T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"18:00:00"}, {"date"=>"2013-10-07T00:00:00", "type"=>2, "checkInTime"=>"15:30:00", "checkOutTime"=>"17:23:00"}]}]}
__END__JSON__

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].each do |clock|
    wday = Date.parse(clock['date']).wday
    cards[name][wday] = true
  end
end

pp cards

出力

{"Anissa Martin"=>[false, true, true, true, true, true, false],
 "Allyson Martin"=>[false, true, true, true, true, true, false],
 "Emmya Burrell"=>[false, true, true, true, true, true, false],
 "Abigail Young"=>[false, true, true, true, true, true, false],
 "William Brown"=>[false, true, true, true, true, true, false],
 "Ramielle Ford"=>[false, false, true, true, true, false, false],
 "Christopher Lampkins"=>[false, true, true, true, true, true, false]}

アップデート

ルイージは を使用することをお勧めしgroup_byます。この代替バージョンはそのように書かれており、読みにくいと思いますが、好むかもしれません。

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(7, false)
  staff['details'].group_by { |clock| Date.parse(clock['date']).wday }.each_key do |wday|
    cards[name][wday] = true;
  end
end

更新 2

この最後の選択肢は、あなたが最近言ったことを実行しているようです。要件が再び変更された場合は、新しい質問を開いて、要件を包括的に説明し、実際のデータの良い例を挙げてください。そこにないバグを探すのに数分費やしました.Anissa Martin は実際8:26 に出勤し、8:00 に出勤しています。

この繰り返しは私のオリジナルからそれほど遠くありません。これを自分で書く機会に耐える十分な知識があれば、同じことを思いつくことができなかったとは信じがたいです.

cards = {}
table['data'].each do |staff|
  name = staff.values_at(*%w/ firstName lastName /).join(' ')
  cards[name] = Array.new(14, nil)
  staff['details'].each do |clock|
    wday = DateTime.parse(clock['date']).wday
    cards[name][wday + wday, 2] = clock.values_at(*%w/ checkInTime checkOutTime /)
  end
end

出力

{"Anissa Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Allyson Martin"=>
  [nil,
   nil,
   "08:26:00",
   "08:00:00",
   "08:19:00",
   "08:00:00",
   "08:15:00",
   "08:00:00",
   "15:30:00",
   "17:40:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Emmya Burrell"=>
  [nil,
   nil,
   "15:30:00",
   "16:40:00",
   "15:30:00",
   "16:00:00",
   "15:30:00",
   "16:00:00",
   "07:00:00",
   "08:00:00",
   "07:00:00",
   "08:00:00",
   nil,
   nil],
 "Abigail Young"=>
  [nil,
   nil,
   "15:30:00",
   "17:26:00",
   "15:30:00",
   "17:00:00",
   "15:30:00",
   "17:36:00",
   "15:30:00",
   "17:30:00",
   "07:30:00",
   "08:00:00",
   nil,
   nil],
 "William Brown"=>
  [nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil],
 "Ramielle Ford"=>
  [nil,
   nil,
   nil,
   nil,
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil,
   nil,
   nil],
 "Christopher Lampkins"=>
  [nil,
   nil,
   "15:30:00",
   "17:23:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   "15:30:00",
   "18:00:00",
   nil,
   nil]}
于 2013-11-09T16:57:33.270 に答える
0

次のことを試してください: array.group_by{|e| Date.parse(e).wday }

http://www.ruby-doc.org/core-2.0.0/Time.html#method-i-wday

于 2013-11-09T17:09:03.003 に答える