Ho una matrice a due elementi di oggetti ActiveRecord, slots_to_import
. Questi oggetti hanno colonne begin_at
e quindi attributi. Stavo cercando di ottenere oggetti con valori unici begin_at
. Ahimè, slots_to_import.uniq_by(&:begin_at)
non ha funzionato. Ma i valori begin_at
sono uguali per i due oggetti:Striatura di ruby: 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]
Qualche altro controllo in giro:
(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]
ho pensato che forse uniq stava controllando se fossero lo stesso oggetto (dal momento che non erano). Ma no, alcuni noodling nella mia console rotaie mi ha mostrato che non fa uso di un controllo oggetto 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]
sto usando Rubino 1.8.7p358 e ActiveSupport 3.2.0. A proposito, posso risolvere il mio problema semplicemente aggiungendo un to_datetime
, ma sono davvero curioso del perché questo non funziona senza una conversione.
Hai provato a confrontare i risultati di 'x.hash' e' y.hash'? – hammar
@hammar: È un'idea interessante vedere come 'uniq_by' è basato su Hash. –
@hammar 'x.hash' e' y.hash' sono uguali! - '(rdb: 1) p slots_to_import.first.begin_at.hash == slots_to_import.last.begin_at.hash => true' – ehsanul