【问题标题】:Ruby Array/include?/.eachRuby 数组/include?/.each
【发布时间】:2013-11-09 15:42:57
【问题描述】:

我在尝试确定如何创建所需的数组时遇到问题。

我有一个看起来像这样的数组:

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

所以有 7 天,10-01 是星期日,10-07 是星期一。我想将每个日期转储到其各自的数组中,但如果日期不存在 (10-03),我想在 tuesday 数组中放置一个空元素。

代码示例

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_array 有 100% 的空值。

【问题讨论】:

  • 你为什么要写这么复杂的代码? :)
  • +1 可能你把整个任务告诉我们,我们提出更好的解决方案?
  • @alex 是的.. 我同意你的看法..

标签: ruby arrays loops


【解决方案1】:

这个程序会按照你的要求去做。它为每个员工创建一个包含false 值的七元素数组,然后将与details 数组的每个元素对应的星期几设置为true。最后,未工作的日子仍然包含false

请注意,Time#wday 在星期日返回 0,在星期六返回 6,因此每个员工数组的成员都是从星期日到星期六。

从输出中可以看出,样本数据中的每个员工都从周一到周五工作,除了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]}

更新

Luigi 建议使用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]}

【讨论】:

  • 这肯定更接近,感谢您的提示。但是,我需要的是一个包含 14 个值的数组,其中包含与每一天相对应的实际时间(签入、签出、签入、签出等)。我怎样才能提取这些数据?
  • 为了澄清我上面的评论,我需要一个像你上面的哈希值是签入/签出时间的 14 个元素数组,与 7 个元素数组(包括真/假)相比。跨度>
  • @Luigi:嗯,这是新的。我建议您需要一组 七对 次,每对都是该期间的进出时间。似乎您还没有考虑到这一点,因为有些员工在一天内登录和退出两次。例如,William Brown每天注销两次。您希望如何表示这些数据?
  • @Luigi,您还需要决定是否要在 前 7 天(在本例中为周二至周一)运行报告,或者列是否始终就像你说的那样是周日到周六。
  • 有效的关注 - 我很抱歉这些数据采用这种格式,实际上在这种情况下我总是选择稍后的时间。最后,此数据将仅包括较晚的时间,因此没有人一天签入/签出的次数不会超过一次。周日至周六的数据将始终在周六晚上运行,因此列将保持静态。
【解决方案2】:

尝试执行以下操作: array.group_by{|e| Date.parse(e).wday }

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

【讨论】:

猜你喜欢
  • 2012-05-02
  • 1970-01-01
  • 2019-03-18
  • 2020-07-16
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
  • 2013-09-28
  • 1970-01-01
相关资源
最近更新 更多