İki elemanlı ActiveRecord nesnesi var, slots_to_import
. Bu nesneler begin_at
sütunlara ve dolayısıyla özelliklere sahiptir. Benzersiz begin_at
değerlerine sahip nesneler almaya çalışıyordum. Ne yazık ki, slots_to_import.uniq_by(&:begin_at)
çalışmadı. Ama begin_at
değerleri iki nesne için eşittir:Ruby tuhaflığı: x == y && [x, y] .uniq == [x, y]
(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]
Biraz daha etrafında kontrol:
(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]
I (onlar değildi beri) belki de uniq aynı nesne olup olmadığı kontrol ediyordu düşündüm.
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]
Ruby 1.8.7p358 ve ActiveSupport 3.2.0 kullanıyorum: Ama hayır, benim raylar konsolda bazı noodling bir nesne kimliği denetimi kullanmayan bana gösterdi. BTW, kendi sorunumu sadece to_datetime
ekleyerek çözebilirim, ancak bunun neden dönüşüm olmadan çalışmadığını merak ediyorum.
'x.hash' ve' y.hash' sonuçlarını karşılaştırmayı denediniz mi? – hammar
@hammar: "uniq_by" Hash tabanlı olarak görülmesi ilginç bir fikir. –
@hammar 'x.hash' ve' y.hash' da eşittir! - '(rdb: 1) p slots_to_import.first.begin_at.hash == slots_to_import.last.begin_at.hash => true ' – ehsanul