<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jesus Castello</title>
    <description>The latest articles on DEV Community by Jesus Castello (@jesus_castello).</description>
    <link>https://dev.to/jesus_castello</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1179%2F4a91aa05-e563-4b2f-85ce-d5a9deae3319.jpg</url>
      <title>DEV Community: Jesus Castello</title>
      <link>https://dev.to/jesus_castello</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jesus_castello"/>
    <language>en</language>
    <item>
      <title>Make Your Ruby Classes More Powerful by Implementing Equality</title>
      <dc:creator>Jesus Castello</dc:creator>
      <pubDate>Sun, 11 Jun 2017 12:20:42 +0000</pubDate>
      <link>https://dev.to/jesus_castello/make-your-ruby-classes-more-powerful-by-implementing-equality</link>
      <guid>https://dev.to/jesus_castello/make-your-ruby-classes-more-powerful-by-implementing-equality</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://www.rubyguides.com/2017/03/ruby-equality/"&gt;rubyguides.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;How do you compare two things in Ruby? Using &lt;code&gt;==&lt;/code&gt; as you already know... but did you know that &lt;code&gt;==&lt;/code&gt; is a method &amp;amp; not just syntax?&lt;/p&gt;

&lt;p&gt;You can implement this method in your own classes to make them more powerful. And that's what I want to talk about in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  Equality Basics
&lt;/h2&gt;

&lt;p&gt;As you know you can compare two strings like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"foo" == "foo"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if the &lt;strong&gt;content&lt;/strong&gt; is equal then this will evaluate to &lt;code&gt;true&lt;/code&gt;. This works because the &lt;code&gt;String&lt;/code&gt; class implements a &lt;code&gt;==&lt;/code&gt; method that knows how to compare strings.&lt;/p&gt;

&lt;p&gt;But what if &lt;code&gt;String&lt;/code&gt; didn't implement &lt;code&gt;==&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Then Ruby would use &lt;code&gt;Object&lt;/code&gt;'s implementation of &lt;code&gt;==&lt;/code&gt;, which defaults to testing for &lt;a href="http://ruby-doc.org/core-2.3.0/Object.html#method-i-object_id"&gt;object identity&lt;/a&gt;, instead of object contents.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="c1"&gt;# false&lt;/span&gt;
&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason &lt;code&gt;Object&lt;/code&gt; returns &lt;code&gt;false&lt;/code&gt; is because two new objects have different object id's.&lt;/p&gt;

&lt;p&gt;In the case of &lt;code&gt;String&lt;/code&gt;, since it compares based on contents, and two new strings have the same content (they are empty) it returns &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Equality
&lt;/h2&gt;

&lt;p&gt;Now let's use what you just learned to make your own classes more powerful by being able to compare them.&lt;/p&gt;

&lt;p&gt;Thanks to the &lt;code&gt;==&lt;/code&gt; method you can define exactly what it means for two instances of your own class to be equal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:price&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;==&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt;  &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;other&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;price&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'book'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'book'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;==&lt;/code&gt; method says that both the name and the price must be the same for two &lt;code&gt;Product&lt;/code&gt; objects to be considered equal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;If you don't implement this method (or use the &lt;code&gt;Comparable&lt;/code&gt; module, which I explain in &lt;a href="https://www.rubyguides.com/ruby-book/"&gt;my Ruby book&lt;/a&gt;) the two objects will be compared using their object id's, instead of their values.&lt;/p&gt;

&lt;p&gt;Also I should mention that if you use a &lt;code&gt;Struct&lt;/code&gt; it already implements &lt;code&gt;==&lt;/code&gt; for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What About Triple Equals?
&lt;/h2&gt;

&lt;p&gt;You may be wondering if &lt;code&gt;==&lt;/code&gt; is a method, is &lt;code&gt;===&lt;/code&gt; also a method? And the answer is yes :)&lt;/p&gt;

&lt;p&gt;So what's the difference between the two?&lt;/p&gt;

&lt;p&gt;In Javascript there is a clear difference, where &lt;code&gt;==&lt;/code&gt; will try to convert the object types to be the same if they aren't (&lt;code&gt;1&lt;/code&gt; vs &lt;code&gt;'1'&lt;/code&gt;). And &lt;code&gt;===&lt;/code&gt; is for 'strict' equality.&lt;/p&gt;

&lt;p&gt;But in Ruby there is not such thing. What &lt;code&gt;===&lt;/code&gt; means depends on the class implementing it.&lt;/p&gt;

&lt;p&gt;In many cases it is just an alias for &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Like in &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a table of built-in classes which give &lt;code&gt;===&lt;/code&gt; a special meaning:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Class&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Range&lt;/td&gt;
&lt;td&gt;Returns true if obj is an element of the range, false otherwise.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Regexp&lt;/td&gt;
&lt;td&gt;Match regexp against a string.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Module&lt;/td&gt;
&lt;td&gt;Returns true if obj is an instance of mod or and instance of one of mod’s descendants.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Proc&lt;/td&gt;
&lt;td&gt;Invokes the block with obj as the proc's parameter like &lt;code&gt;Proc#call&lt;/code&gt;. It is to allow a proc object to be a target of a &lt;code&gt;when&lt;/code&gt; clause in a &lt;a href="https://www.blackbytes.info/2015/10/ruby-case/"&gt;case statement&lt;/a&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this post you learned how to make your classes more powerful by implementing the &lt;code&gt;==&lt;/code&gt; method. You also learned the difference between &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;===&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you enjoyed this post &lt;a href="https://www.blackbytes.info/"&gt;check out my blog here&lt;/a&gt; &amp;amp; sign-up to my newsletter :)&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
    <item>
      <title>Hi, I'm Jesus Castello</title>
      <dc:creator>Jesus Castello</dc:creator>
      <pubDate>Tue, 03 Jan 2017 20:48:57 +0000</pubDate>
      <link>https://dev.to/jesus_castello/hi-im-jesus-castello</link>
      <guid>https://dev.to/jesus_castello/hi-im-jesus-castello</guid>
      <description>&lt;p&gt;I have been coding for 15 years.&lt;/p&gt;

&lt;p&gt;You can find me on GitHub as &lt;a href="https://github.com/matugm" rel="noopener noreferrer"&gt;matugm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I live in Spain.&lt;/p&gt;

&lt;p&gt;I mostly program in Ruby.&lt;/p&gt;

&lt;p&gt;My blog &lt;a href="http://www.rubyguides.com" rel="noopener noreferrer"&gt;can be found here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I am currently learning more about Business &amp;amp; Marketing.&lt;/p&gt;

&lt;p&gt;Nice to meet you!&lt;/p&gt;

</description>
      <category>introduction</category>
    </item>
  </channel>
</rss>
