<?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: Noura</title>
    <description>The latest articles on DEV Community by Noura (@nouraloudani).</description>
    <link>https://dev.to/nouraloudani</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%2F523465%2F5606cbf3-8e89-49f1-8f3b-c291b569fb10.jpeg</url>
      <title>DEV Community: Noura</title>
      <link>https://dev.to/nouraloudani</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nouraloudani"/>
    <language>en</language>
    <item>
      <title>Nothingness &amp; Truth in Ruby Explained</title>
      <dc:creator>Noura</dc:creator>
      <pubDate>Tue, 01 Dec 2020 07:12:59 +0000</pubDate>
      <link>https://dev.to/nouraloudani/nothingness-truth-in-ruby-explained-1hig</link>
      <guid>https://dev.to/nouraloudani/nothingness-truth-in-ruby-explained-1hig</guid>
      <description>&lt;p&gt;As a coding newbie, I still question and sometimes overthink concepts that might seem pretty basic to most programmers. During my pre-bootcamp preparation journey, one of the topics I had to focus on was Ruby Truthiness and Falseness. It triggered my curiosity.&lt;/p&gt;

&lt;p&gt;After doing some research, asking questions to my Flatiron School instructors, and discussing with my boyfriend, I discovered the contrast between Ruby's truthiness versus other languages. &lt;/p&gt;

&lt;p&gt;Let's go over some questions and present examples to help explain how it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why should booleans never confuse you in Ruby?&lt;/strong&gt;&lt;br&gt;
If you look at the cover image of this article, you should be able to identify how many objects there were on the different walls. There was nothing or in Ruby terms, &lt;strong&gt;nil&lt;/strong&gt;. &lt;code&gt;nil&lt;/code&gt; represents nothingness. It is also considered "falsy". Yuuup! It's because it is empty. So, is there any other natively falsy object? NO, except for &lt;code&gt;false&lt;/code&gt; itself, everything else in Ruby is "truthy". In contrast to other languages like JavaScript, where empty strings &lt;code&gt;""&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt; are falsy. Ruby considers them truthy because they still represent something and not nothing.&lt;/p&gt;

&lt;p&gt;Truthiness and Falseness are language dependent based on what the implementers chose.&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;def&lt;/span&gt; &lt;span class="nf"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;var_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;is_truthy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;"truthy"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"falsy"&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;var_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;is_truthy&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"false"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"nil"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"empty string"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"empty array"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;
  &lt;span class="n"&gt;check_truthy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"empty hash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;=&amp;gt; &lt;code&gt;false is falsy&lt;br&gt;
nil is falsy&lt;br&gt;
0 is truthy&lt;br&gt;
empty string is truthy&lt;br&gt;
\n is truthy&lt;br&gt;
empty array is truthy&lt;br&gt;
empty hash is truthy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When do we receive nil?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;nil&lt;/code&gt; is a special object in Ruby. When we call a method, we know we are going to get something in return. right? Sometimes, when there is nothing to return, it will return &lt;code&gt;nil&lt;/code&gt;, which represents nothing.&lt;br&gt;
Just like our instructor "Kim" would say: "If it sounds like I am repeating my self. It is because I am".&lt;/p&gt;

&lt;p&gt;We could go on and on the philosophical implications of nil, but instead, we’ll just look at how it is used in practice:&lt;/p&gt;

&lt;p&gt;Let's say we have this magazines hash;&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="n"&gt;magazines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:fashion&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"style"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:sport&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"gogo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:travel&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"vacation"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;magazines&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:sport&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will print out &lt;code&gt;"gogo"&lt;/code&gt;. However, what happens if we try to receive the value for a key that has not been defined on the hash?&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="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;magazines&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:health&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return;&lt;/p&gt;

&lt;p&gt;'nil'&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What kind of methods return a boolean?&lt;/strong&gt;&lt;br&gt;
You might have seen some methods that ends with a question mark.&lt;/p&gt;

&lt;p&gt;Example: .even?&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;even?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;=&amp;gt; &lt;code&gt;true&lt;/code&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;even?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;=&amp;gt; &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We call these "Boolean Methods" or "Predicate Methods", because, they return a boolean value. Conventionally, the methods that end with a question mark are reserved for boolean methods.&lt;/p&gt;

&lt;p&gt;Apart from the Ruby built-in boolean methods, we can also create our own.&lt;br&gt;
Watch:&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;def&lt;/span&gt; &lt;span class="nf"&gt;multiples_of_7?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;multiples_of_7?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;=&amp;gt; &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we try the same method, passing 5 as an argument, it would then output &lt;code&gt;false&lt;/code&gt;. Why? Simply because 5 isn't a multiple of 7.&lt;/p&gt;

&lt;p&gt;Overall, booleans are simple. Although, it could get tricky in some occasions. Make sure you are able to differentiate between TrueClass class (class that groups true boolean objects) and a FalseClass class (class that groups false boolean objects). Be aware of emptiness and ruby specifics.&lt;/p&gt;

&lt;p&gt;Happy coding beginnings!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>firstblog</category>
      <category>flatironschool</category>
      <category>truthiness</category>
    </item>
  </channel>
</rss>
