【发布时间】:2015-11-16 09:38:19
【问题描述】:
class CustomSorter
attr_accessor :start_date, :available
def initialize(start_date, available)
@start_date = Time.mktime(*start_date.split('-'))
@available = available
end
end
cs1 = CustomSorter.new('2015-08-01', 2)
cs2 = CustomSorter.new('2015-08-02', 1)
cs3 = CustomSorter.new('2016-01-01', 1)
cs4 = CustomSorter.new('2015-02-01', 3)
cs5 = CustomSorter.new('2015-03-01', 4)
sorted = [cs1, cs2, cs3, cs4, cs5].sort_by { |cs| [Time.now <= cs.start_date, (cs.available || 0)] }
puts sorted.map(&:start_date)
但它失败了:
custom_sorter.rb:17:in `sort_by': Array 与 Array 的比较失败 (ArgumentError)
我知道 nil 可能会产生这个错误。但是我的数据中没有零。
【问题讨论】:
-
你有什么问题?
-
我已经在控制台中粘贴了上面的代码并且执行没有错误。请帮我重现这个问题。因为我在生产中遇到了这个问题,但在本地服务器上没有。
标签: ruby