Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby weirdness: x == y && [x, y].uniq == [x, y]
    primarykey
    data
    text
    <p>I have an two-element array of ActiveRecord objects, <code>slots_to_import</code>. These objects have <code>begin_at</code> columns, and thus attributes. I was trying to get objects that have unique <code>begin_at</code> values. Alas, <code>slots_to_import.uniq_by(&amp;:begin_at)</code> did not work. But the <code>begin_at</code> values are equal for the two objects:</p> <pre><code>(rdb:1) p slots_to_import.first.begin_at == slots_to_import.last.begin_at true (rdb:1) p slots_to_import.uniq_by(&amp;:begin_at).map(&amp;: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] </code></pre> <p>Some more checking around:</p> <pre><code>(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] </code></pre> <p>I thought perhaps that uniq was checking whether they were the same object (since they were not). But no, some noodling in my rails console showed me that it does not use an object id check:</p> <pre><code>1.8.7 :111 &gt; x = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00") =&gt; Mon, 29 Oct 2012 19:29:17 UTC +00:00 1.8.7 :112 &gt; y = Time.zone.parse("Mon, 29 Oct 2012 19:29:17 UTC +00:00") =&gt; Mon, 29 Oct 2012 19:29:17 UTC +00:00 1.8.7 :113 &gt; x == y =&gt; true 1.8.7 :114 &gt; [x, y].uniq =&gt; [Mon, 29 Oct 2012 19:29:17 UTC +00:00] </code></pre> <p>I'm using Ruby 1.8.7p358 and ActiveSupport 3.2.0. BTW, I can solve my own issue by simply adding a <code>to_datetime</code>, but I'm really curious why this isn't working without a conversion.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload