我正在制作基于排列的锦标赛时间表,但我希望能够通过新的比赛更新时间表,如果新玩家加入联盟的话。
我的代码如下所示,目前抛出错误:
"in update': undefined method `[]' for 2014-04-04 11:00:00 +0200:Time (NoMethodError)"
require 'pp'
require 'set'
def schedule(players)
matchups = players.permutation(2)
matches = []
matchups.each do |matchup|
unless matches.include?(matchup.reverse)
matches << matchup
end
end
matches.each do |match|
match << time
end
matches
end
def time
now = Time.now
a_week_ahead = now + 60 * 60 * 24 * 7
random_time = rand(now..a_week_ahead)
random_time -= random_time.min * 60 + random_time.sec
end
# The map methods is to cut off the date for each match when checking for duplicates
def update(original, updated)
updated.each do |match|
original << match unless original.map{ |match| match[0..1]}.include?(match.map{ |match| match[0..1] })
end
original
end
update(schedule(('A'..'H').to_a), schedule(('A'..'I').to_a))
非常感谢任何帮助!
答案 0 :(得分:0)
match.map
后include?
返回数组的所有三个元素,包括日期。
尝试此操作 - 在match
条件中将include?
创建为数组数组:
def update(original, updated)
updated.each do |match|
original << match unless original.map{ |match| match[0..1]}.include?([match].map{ |match| match[0..1] })
end
original
end
只是一个编辑 - 我认为这应该会给你相同的结果,如果没有第二个map
,它会更轻松:
def update(original, updated)
updated.each do |match|
original << match unless original.map{ |match| match[0..1]}.include?(match[0..1])
end
original
end