Ruby古怪:x == y && [x,y] .uniq == [x,y]

ehs*_*nul 7 ruby datetime ruby-on-rails activesupport

我有一个两元素的ActiveRecord对象数组slots_to_import.这些对象具有begin_at列,因此具有属性.我试图获得具有唯一begin_at值的对象.唉,没用slots_to_import.uniq_by(&:begin_at).但begin_at两个对象的值相等:

(rdb:1) p slots_to_import.first.begin_at == slots_to_import.last.begin_at
true
(rdb:1) p slots_to_import.uniq_by(&:begin_at).map(&:begin_at)
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
(rdb:1) p [slots_to_import.first.begin_at, slots_to_import.last.begin_at].uniq
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
Run Code Online (Sandbox Code Playgroud)

还有一些检查:

(rdb:1) p [slots_to_import.first.begin_at.to_datetime, slots_to_import.last.begin_at.to_datetime].uniq
[Mon, 26 Nov 2012 19:00:00 +0000]
(rdb:1) p [slots_to_import.first.begin_at.usec, slots_to_import.last.begin_at.usec].uniq
[0]
(rdb:1) p [slots_to_import.first.begin_at.to_f, slots_to_import.last.begin_at.to_f].uniq
[1353956400.0]
(rdb:1) p [slots_to_import.first.begin_at.utc, slots_to_import.last.begin_at.utc].uniq
[Mon, 26 Nov 2012 19:00:00 +0000]
(rdb:1) p [slots_to_import.first.begin_at, slots_to_import.last.begin_at].uniq
[Mon, 26 Nov 2012 19:00:00 UTC +00:00, Mon, 26 Nov 2012 19:00:00 UTC +00:00]
Run Code Online (Sandbox Code Playgroud)

我想也许uniq正在检查它们是否是同一个对象(因为它们不是).但不,我的rails控制台中的一些noodling告诉我它不使用对象id检查:

1.8.7 :111 > x = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00")
 => Mon, 29 Oct 2012 19:29:17 UTC +00:00 
1.8.7 :112 > y = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00")
 => Mon, 29 Oct 2012 19:29:17 UTC +00:00 
1.8.7 :113 > x == y
 => true 
1.8.7 :114 > [x, y].uniq
 => [Mon, 29 Oct 2012 19:29:17 UTC +00:00] 
Run Code Online (Sandbox Code Playgroud)

我使用的是Ruby 1.8.7p358和ActiveSupport 3.2.0.顺便说一下,我可以通过简单地添加一个来解决我自己的问题to_datetime,但我真的很好奇为什么没有转换就无法工作.

pgu*_*rio 0

我的猜测是不同的微秒。2 个对象可以显示相同的检查字符串,但不相等。