<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Ruby Equality And Object Comparison</title> <atom:link href="http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/feed/" rel="self" type="application/rss+xml" /><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/</link> <description>For the betterment of the software craft...</description> <lastBuildDate>Mon, 21 Nov 2011 13:57:06 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.2</generator> <item><title>By: The more you know &#124; steveshaw.ca</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7267</link> <dc:creator>The more you know &#124; steveshaw.ca</dc:creator> <pubDate>Mon, 01 Aug 2011 21:45:57 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7267</guid> <description>[...] define a comparison in the whatever.rb model class: def &lt;=&gt; &#040;another&#041; self.name &lt;=&gt; another.name [...]</description> <content:encoded><![CDATA[<p>[...] define a comparison in the whatever.rb model class: def &lt;=&gt; &#040;another&#041; self.name &lt;=&gt; another.name [...]</p> ]]></content:encoded> </item> <item><title>By: Pedro Salgado</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7107</link> <dc:creator>Pedro Salgado</dc:creator> <pubDate>Wed, 27 Apr 2011 17:27:28 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7107</guid> <description>(on my 3 previous comments, some characters were removed so here is another attempt)
A shorter version and more complete version of the code above could be done using
def &lt;=&gt;(another_sock)
return self.size &lt;=&gt; another_sock.size
end
I also found that sometimes you may want to compare against other classes so this would work better.
def &lt;=&gt;(other)
if other.instance_of? self.class
return self.size &lt;=&gt; other.size
elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
return self.size &lt;=&gt; other
end
raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])
end</description> <content:encoded><![CDATA[<p>(on my 3 previous comments, some characters were removed so here is another attempt)<br
/> A shorter version and more complete version of the code above could be done using</p><p>def &lt;=&gt;(another_sock)<br
/> return self.size &lt;=&gt; another_sock.size<br
/> end</p><p>I also found that sometimes you may want to compare against other classes so this would work better.</p><p>def &lt;=&gt;(other)<br
/> if other.instance_of? self.class<br
/> return self.size &lt;=&gt; other.size<br
/> elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float<br
/> return self.size &lt;=&gt; other<br
/> end<br
/> raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])<br
/> end</p> ]]></content:encoded> </item> <item><title>By: Pedro Salgado</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7106</link> <dc:creator>Pedro Salgado</dc:creator> <pubDate>Wed, 27 Apr 2011 17:27:04 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7106</guid> <description>(on my 2 previous comments, some characters were removed so here is another attempt)
A shorter version and more complete version of the code above could be done using
def &lt;=&gt;(another_sock)
return self.size &lt;=&gt; another_sock.size
end
I also found that sometimes you may want to compare against other classes so this would work better.
def (other)
if other.instance_of? self.class
return self.size &lt;=&gt; other.size
elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
return self.size &lt;=&gt; other
end
raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])
end</description> <content:encoded><![CDATA[<p>(on my 2 previous comments, some characters were removed so here is another attempt)<br
/> A shorter version and more complete version of the code above could be done using</p><p>def &lt;=&gt;(another_sock)<br
/> return self.size &lt;=&gt; another_sock.size<br
/> end</p><p>I also found that sometimes you may want to compare against other classes so this would work better.</p><p>def (other)<br
/> if other.instance_of? self.class<br
/> return self.size &lt;=&gt; other.size<br
/> elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float<br
/> return self.size &lt;=&gt; other<br
/> end<br
/> raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])<br
/> end</p> ]]></content:encoded> </item> <item><title>By: Pedro Salgado</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7105</link> <dc:creator>Pedro Salgado</dc:creator> <pubDate>Wed, 27 Apr 2011 17:26:29 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7105</guid> <description>(on my previous comment, some characters were removed so here is another attempt)
A shorter version and more complete version of the code above could be done using
def (another_sock)
return self.size &lt;=&gt; another_sock.size
end
I also found that sometimes you may want to compare against other classes so this would work better.
def (other)
if other.instance_of? self.class
return self.size &lt;=&gt; other.size
elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
return self.size &lt;=&gt; other
end
raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])
end</description> <content:encoded><![CDATA[<p>(on my previous comment, some characters were removed so here is another attempt)</p><p>A shorter version and more complete version of the code above could be done using</p><p>def (another_sock)<br
/> return self.size &lt;=&gt; another_sock.size<br
/> end</p><p>I also found that sometimes you may want to compare against other classes so this would work better.</p><p>def (other)<br
/> if other.instance_of? self.class<br
/> return self.size &lt;=&gt; other.size<br
/> elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float<br
/> return self.size &lt;=&gt; other<br
/> end<br
/> raise ArgumentError.new(‘comparison of %s with %s failed!’ % [self.class, other.class])<br
/> end</p> ]]></content:encoded> </item> <item><title>By: Pedro Salgado</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7104</link> <dc:creator>Pedro Salgado</dc:creator> <pubDate>Wed, 27 Apr 2011 17:24:10 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7104</guid> <description>A shorter version and more complete version of the code above could be done using
def (another_sock)
return self.size  another_sock.size
end
I also found that sometimes you may want to compare against other classes so this would work better.
def (other)
if other.instance_of? self.class
return self.size  other.size
elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float
return self.size  other
end
raise ArgumentError.new(&#039;comparison of %s with %s failed!&#039; % [self.class, other.class])
end</description> <content:encoded><![CDATA[<p>A shorter version and more complete version of the code above could be done using</p><p> def (another_sock)<br
/> return self.size  another_sock.size<br
/> end</p><p>I also found that sometimes you may want to compare against other classes so this would work better.</p><p> def (other)<br
/> if other.instance_of? self.class<br
/> return self.size  other.size<br
/> elsif other.instance_of? Fixnum or other.instance_of? Integer or other.instance_of? Float<br
/> return self.size  other<br
/> end<br
/> raise ArgumentError.new(&#8216;comparison of %s with %s failed!&#8217; % [self.class, other.class])<br
/> end</p> ]]></content:encoded> </item> <item><title>By: Sahil Verma</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-7073</link> <dc:creator>Sahil Verma</dc:creator> <pubDate>Thu, 31 Mar 2011 16:33:02 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-7073</guid> <description>So in summary regarding equality:
* equal? is reference equality
* == is value equality
* eql? is value and type equality</description> <content:encoded><![CDATA[<p>So in summary regarding equality:</p><p> * equal? is reference equality<br
/> * == is value equality<br
/> * eql? is value and type equality</p> ]]></content:encoded> </item> <item><title>By: Tarun Mittal</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-6130</link> <dc:creator>Tarun Mittal</dc:creator> <pubDate>Tue, 07 Sep 2010 10:24:46 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-6130</guid> <description>A great post mann.. Cleared almost all my doubts.. And comment by  apeiros was actually useful. Am new to ruby, and have started reading your posts. Thanks a lot :)</description> <content:encoded><![CDATA[<p>A great post mann.. Cleared almost all my doubts.. And comment by  apeiros was actually useful. Am new to ruby, and have started reading your posts. Thanks a lot :)</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-3149</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Fri, 02 Oct 2009 05:30:56 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-3149</guid> <description>I see what you&#039;re getting, like I said I am not a rails expert (yet :)), so was just giving my view on what I thought was happening, would be happy to hear a definitive answer from someone with skillz :).</description> <content:encoded><![CDATA[<p>I see what you&#8217;re getting, like I said I am not a rails expert (yet :)), so was just giving my view on what I thought was happening, would be happy to hear a definitive answer from someone with skillz :).</p> ]]></content:encoded> </item> <item><title>By: Mat Brown</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-3148</link> <dc:creator>Mat Brown</dc:creator> <pubDate>Fri, 02 Oct 2009 04:45:15 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-3148</guid> <description>Not keeping an object store for performance reasons? I&#039;m not sure how performance benefits by re-instantiating the same object over and over - which may be why every (other?) serious ORM I&#039;ve worked with guarantees one instance per row per session. And have fun with situations like this:
blog.name = &quot;My Blog&quot;
blog.posts.first.blog.name #=&gt; Not &quot;My Blog&quot;!</description> <content:encoded><![CDATA[<p>Not keeping an object store for performance reasons? I&#8217;m not sure how performance benefits by re-instantiating the same object over and over &#8211; which may be why every (other?) serious ORM I&#8217;ve worked with guarantees one instance per row per session. And have fun with situations like this:</p><p> blog.name = &#8220;My Blog&#8221;<br
/> blog.posts.first.blog.name #=&gt; Not &#8220;My Blog&#8221;!</p> ]]></content:encoded> </item> <item><title>By: Alan Skorkin</title><link>http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/comment-page-1/#comment-2821</link> <dc:creator>Alan Skorkin</dc:creator> <pubDate>Thu, 17 Sep 2009 08:57:00 +0000</pubDate> <guid
isPermaLink="false">http://www.skorks.com/?p=1195#comment-2821</guid> <description>I am not a Rails expert, but from what I see this is what I think is happening. I don&#039;t believe active record keeps a table of references to objects which should be the same objects. Here is what I mean:
1) As far as your == method is concerned they are the same object
2) When you look up a parent object from a child object, active record does not have an internal table to know that the parent object you get from the child is pointing to the same object as the parent object you already have
3) Essentially whenever you get to a particular object through a different means, what you&#039;re actually seeing will be a clone of the object as far as Ruby is concerned, so it will have a different object id
4) As far as Active Record is concerned, it is the same object and if you look up the database id property on the two objects, just like you did the object_id property I think you will find they are the same. Which is why == reports true
I wouldn&#039;t worry too much about it, chances are they are doing it this way for performance reasons. If I think about it I can see how it may be difficult to implement active record in such a way as to keep object_id consistent.</description> <content:encoded><![CDATA[<p>I am not a Rails expert, but from what I see this is what I think is happening. I don&#8217;t believe active record keeps a table of references to objects which should be the same objects. Here is what I mean:</p><p>1) As far as your == method is concerned they are the same object<br
/> 2) When you look up a parent object from a child object, active record does not have an internal table to know that the parent object you get from the child is pointing to the same object as the parent object you already have<br
/> 3) Essentially whenever you get to a particular object through a different means, what you&#8217;re actually seeing will be a clone of the object as far as Ruby is concerned, so it will have a different object id<br
/> 4) As far as Active Record is concerned, it is the same object and if you look up the database id property on the two objects, just like you did the object_id property I think you will find they are the same. Which is why == reports true</p><p>I wouldn&#8217;t worry too much about it, chances are they are doing it this way for performance reasons. If I think about it I can see how it may be difficult to implement active record in such a way as to keep object_id consistent.</p> ]]></content:encoded> </item> </channel> </rss>
