title: Guaranteed way to compare objects by identity in Ruby
body: <p>What is a guaranteed way to compare two objects by their identity in Ruby?  Given two variables, I want to return true if the variables point to the exact same object in memory.</p>
  
  <p>For most Ruby objects, the <code>equal?</code> method compares by identity:</p>
  
  <pre><code>f = g = Object.new
  p f.equal? g  # =&gt; true
  </code></pre>
  
  <p>However, this doesn't work for all objects.  For example:</p>
  
  <pre><code>class F
    def ==(obj) false end
    def ===(obj) false end
    def eql?(obj) false end
    def equal?(obj) false end
    def object_id; self end
  end
  
  f = g = F.new
  p f == g       # =&gt; false
  p f === g      # =&gt; false
  p f.eql? g     # =&gt; false
  p f.equal? g   # =&gt; false
  p f.object_id == g.object_id  # =&gt; false
  </code></pre>
  
  <p>What is a foolproof/guaranteed way of comparing two objects by identity which can't be defeated?</p>
  
  <p>This is a purely intellectual question.  The answer to any question that begins with "why" will probably be "Because I am curious."</p>