Mam dwuelementową tablicę obiektów ActiveRecord, slots_to_import
. Obiekty te mają kolumny begin_at
, a więc atrybuty. Próbowałem uzyskać obiekty, które mają unikalne wartości begin_at
. Niestety, slots_to_import.uniq_by(&:begin_at)
nie działa. Ale wartości begin_at
są równe dla obu obiektów:Dziwactwo rubowe: 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]
Niektóre bardziej sprawdzanie wokół:
(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]
myślałem chyba że uniq było sprawdzenie, czy były one ten sam przedmiot (ponieważ nie byli). Ale nie, niektóre noodling w moim konsoli szyn pokazał mi, że nie używa czek przedmiot 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]
Używam Ruby 1.8.7p358 i ActiveSupport 3.2.0. BTW, mogę rozwiązać mój problem, po prostu dodając to_datetime
, ale jestem naprawdę ciekawy, dlaczego to nie działa bez konwersji.
Czy próbowałeś porównać wyniki 'x.hash' i' y.hash'? – hammar
@hammar: To interesujący pomysł, ponieważ 'uniq_by' jest oparty na Hash. –
@hammar 'x.hash' i' y.hash' są równe! - '(rdb: 1) p slots_to_import.first.begin_at.hash == slots_to_import.last.begin_at.hash => true' – ehsanul