<?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: 0x-2FA</title>
    <description>The latest articles on DEV Community by 0x-2FA (@0x2fa).</description>
    <link>https://dev.to/0x2fa</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%2F783507%2F9cf617f0-4746-40d1-b871-256c6810e79c.jpeg</url>
      <title>DEV Community: 0x-2FA</title>
      <link>https://dev.to/0x2fa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/0x2fa"/>
    <language>en</language>
    <item>
      <title>100 Days of Learning Ruby: Day #4</title>
      <dc:creator>0x-2FA</dc:creator>
      <pubDate>Mon, 10 Jan 2022 20:25:22 +0000</pubDate>
      <link>https://dev.to/0x2fa/100-days-of-learning-ruby-day-4-3h3h</link>
      <guid>https://dev.to/0x2fa/100-days-of-learning-ruby-day-4-3h3h</guid>
      <description>&lt;h2&gt;
  
  
  OOP in Ruby (Part II)
&lt;/h2&gt;

&lt;p&gt;Last time we learnt what is Object Oriented Programming and we started to learn how it is implemented in Ruby, today we will continue learning about OOP in Ruby.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is attr_accessor, attr_reader and attr_writer?&lt;/li&gt;
&lt;li&gt;Inheritance in Ruby&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setup for Today
&lt;/h3&gt;

&lt;p&gt;Create a new folder called Day_4, then create a new file with the name oop2.rb and finally open it up in a text editor of your choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;100_days_of_ruby/
&lt;span class="nb"&gt;mkdir &lt;/span&gt;Day_4 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;Day_4/ &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;touch &lt;/span&gt;oop2.rb
codium &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is attr_accessor, attr_reader and attr_writer?
&lt;/h3&gt;

&lt;p&gt;Last time we created the following class:&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;Dog&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="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# return the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# set the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;SetAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we used the &lt;code&gt;SetAge&lt;/code&gt; method to set the dog's age and the &lt;code&gt;GetAge&lt;/code&gt; to retrieve the dog's age. But why create these methods ourselves when Ruby can do it for us.&lt;/p&gt;

&lt;p&gt;To understand that let's firstly see that we can't just simply access the &lt;code&gt;age&lt;/code&gt; attribute/variable but we need to call the &lt;code&gt;GetAge&lt;/code&gt; or &lt;code&gt;SetAge&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;For example add the above class to our file and then below that add the following lines of code:&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&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;# creating a new Dog object&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;   &lt;span class="c1"&gt;# trying to print the age of our object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you try to run this code you will end up with an error saying &lt;code&gt;undefined method 'age'&lt;/code&gt;. We cannot &lt;em&gt;access&lt;/em&gt; the attribute &lt;code&gt;age&lt;/code&gt; without calling our GetAge method.&lt;/p&gt;

&lt;p&gt;Let's modify our Dog class a bit:&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;Dog&lt;/span&gt;

  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:age&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="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&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;# creating a new Dog object&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;    &lt;span class="c1"&gt;# printing the initialized age of our dog object&lt;/span&gt;

&lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;     &lt;span class="c1"&gt;# modifying the age attribute&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;age&lt;/span&gt;    &lt;span class="c1"&gt;# printing the new age of our dog object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run our program we can see that we have no errors. Also we can see that we removed our &lt;code&gt;SetAge&lt;/code&gt; and &lt;code&gt;GetAge&lt;/code&gt; methods and we added the line &lt;code&gt;attr_accessor :age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So what happened is that when we declare a class attribute with &lt;code&gt;attr_accessor&lt;/code&gt; Ruby will automatically create a Getter and a  Setter method for that attribute. &lt;/p&gt;

&lt;p&gt;If we only wanna have a Getter for an attribute then we declare it with &lt;code&gt;attr_reader&lt;/code&gt; and if we only wanna have a Setter for an attribute we declare it with &lt;code&gt;attr_writer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Add the following lines of code to our class to demonstrate the above:&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;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:name&lt;/span&gt;
&lt;span class="nb"&gt;attr_writer&lt;/span&gt; &lt;span class="ss"&gt;:dob&lt;/span&gt;  &lt;span class="c1"&gt;# dob = date of birth&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="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Snoopy'&lt;/span&gt;
  &lt;span class="vi"&gt;@dob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'13-06-2003'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go ahead and print the &lt;code&gt;name&lt;/code&gt; and the &lt;code&gt;dob&lt;/code&gt; of the Dog object. You can print the attribute &lt;code&gt;name&lt;/code&gt; cause it is declared with &lt;code&gt;attr_reader&lt;/code&gt; but you can't print the attribute &lt;code&gt;dob&lt;/code&gt; cause it's declared with &lt;code&gt;attr_writer&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inheritance in Ruby
&lt;/h3&gt;

&lt;p&gt;This blog is already kinda lengthy and I don't wanna over do it so I'm not gonna go over what Inheritance is, I'm just gonna show you how it's done in Ruby.&lt;/p&gt;

&lt;p&gt;Add the newly created Animal class before the Dog class and change the class Dog slightly:&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;Animal&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I like to eat &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;food&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm going to sleep."&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;Drink&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I drink lots of water"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Animal&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:age&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="nb"&gt;attr_writer&lt;/span&gt; &lt;span class="ss"&gt;:dob&lt;/span&gt;  &lt;span class="c1"&gt;# dob = date of birth&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="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
    &lt;span class="vi"&gt;@name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Snoopy'&lt;/span&gt;
    &lt;span class="vi"&gt;@dob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'13-06-2003'&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in Ruby a class can inherit the methods and the attributes of another class with the symbol &lt;code&gt;&amp;lt;&lt;/code&gt;. &lt;br&gt;
Here we can see that the class Dog inherits 3 methods from the class Animal. We can now modify our program again to use these functions from a Dog object.&lt;/p&gt;

&lt;p&gt;Add the following to the end of our program:&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;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Eat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Meat"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Drink&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;Sleep&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will end Day #4 here but again we haven't learnt everything about OOP in Ruby, I decided to cover more advanced OOP concepts a bit later, so some time in the series there is going to be a Part III. Next time we will learn about the Control Flow of our Ruby program (if statements, loops etc).&lt;/p&gt;

&lt;p&gt;I was a bit hasty with this one cause I don't have much time to practice and learn ruby these days. That's also the reason that I don't create these blogs more often.&lt;/p&gt;

&lt;p&gt;Anayways thanks for joining, I will see you next time!&lt;/p&gt;

</description>
      <category>100daysofcode</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>100 Days of Learning Ruby: Day #3</title>
      <dc:creator>0x-2FA</dc:creator>
      <pubDate>Tue, 04 Jan 2022 16:46:54 +0000</pubDate>
      <link>https://dev.to/0x2fa/100-days-of-learning-ruby-day-3-4j7d</link>
      <guid>https://dev.to/0x2fa/100-days-of-learning-ruby-day-3-4j7d</guid>
      <description>&lt;p&gt;Originally when I was writing this blog I was going through what is Object Oriented programming (OOP) and then how we do it in ruby but the blog post became lengthy and I'm not here to teach programming. I'm just sharing my journey of learning Ruby in a tutorial style.&lt;/p&gt;

&lt;p&gt;So instead I provided some links that can help you learn everything I was trying to teach you about OOP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 3: Understanding Ruby a bit more
&lt;/h2&gt;

&lt;p&gt;Now that we have the very very basics of ruby down it's time to learn more about it. Today we are going to learn the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Object Oriented Programming (OOP)&lt;/li&gt;
&lt;li&gt;OOP in Ruby (Part I)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setup for Today
&lt;/h2&gt;

&lt;p&gt;Create a new folder called Day_3, then create a new file with the name oop.rb and finally open it up in a  text editor of your choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;100_days_of_ruby/
&lt;span class="nb"&gt;mkdir &lt;/span&gt;Day_3 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;Day_3/ &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;touch &lt;/span&gt;oop.rb
codium &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Introduction to Object Oriented Programming (OOP)
&lt;/h2&gt;

&lt;p&gt;Everyone has heard of Object Oriented Programming and probably everyone has used it at least one in their career. So let's learn some things about it.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Object Oriented Programming (OOP)?
&lt;/h3&gt;

&lt;p&gt;The wiki has a nice definition:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Object-oriented programming (OOP) is a programming paradigm based on the concept of &lt;em&gt;objects&lt;/em&gt;, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There is some fancy terminology here such as &lt;em&gt;Programming Paradigm&lt;/em&gt; and we need to understand that first to be able to learn about OOP.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Programming Paradigm?
&lt;/h3&gt;

&lt;p&gt;Basically a programming paradigm is the concept by which the methodology of a programming language adheres to. Paradigms are important because they define a programming language and how it works.&lt;/p&gt;

&lt;p&gt;There are a lot of paradigms out in the wild and in fact ruby is a multi paradigm language (aka supports more than one paradigm). We are not gonna discuss other programming paradigms today but here is an amazing site where you can learn more about them &lt;a href="https://cs.lmu.edu/~ray/notes/paradigms/"&gt;https://cs.lmu.edu/~ray/notes/paradigms/&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basic Idea of OOP
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://medium.com/@cancerian0684/what-are-four-basic-principles-of-object-oriented-programming-645af8b43727"&gt;What are four basic principles of Object Oriented Programming?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.indeed.com/career-advice/career-development/what-is-object-oriented-programming"&gt;What Is Object-Oriented Programming? The Four Basic Concepts of OOP&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  OOP in Ruby (Part I)
&lt;/h2&gt;

&lt;p&gt;In most (if not all) ruby programs everything depends on objects. In fact most of the time in ruby you are creating new objects without even knowing it and that's because &lt;strong&gt;everything in ruby is an object&lt;/strong&gt;. Also every object is obviously an instance of a class.&lt;/p&gt;

&lt;p&gt;We can test that by adding the following lines of code to our program:&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;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upcase&lt;/span&gt; &lt;span class="c1"&gt;# prints HELLO, WORLD!&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, World!"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;  &lt;span class="c1"&gt;# prints String&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, we called a method on a plain string and that's because this "plain" string is in fact an object and it's an object of the class String.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining our own Classes in Ruby
&lt;/h3&gt;

&lt;p&gt;This is very simple, just add the following to our program:&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;Dog&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We just created our own class with the name Dog, it doesn't have any code yet but we will gradually add more things to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining our own Methods
&lt;/h3&gt;

&lt;p&gt;Methods are super important to any Ruby class. Let's create our own by adding the following lines to our Dog class:&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;Dog&lt;/span&gt;

  &lt;span class="c1"&gt;# return the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we defined a new method called &lt;code&gt;GetAge&lt;/code&gt; that at the moment doesn't do anything.&lt;/p&gt;

&lt;p&gt;The way we define methods in ruby is pretty easy. Start with the &lt;code&gt;def&lt;/code&gt; keyword followed by the name of your method, it can be anything you want, then a number of parameters and finally to close it we use the keyword &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Keep in mind that we don't always need to have parameters to every method. In our case the method &lt;code&gt;GetAge()&lt;/code&gt; doesn't have any parameters cause it simply doesn't need one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Generally you can name your methods however you want but don't name them &lt;code&gt;initialize&lt;/code&gt; that's a special method that we will learn a bit later.&lt;/p&gt;

&lt;p&gt;Also method names in ruby are generally written in &lt;code&gt;snake_case&lt;/code&gt; so it would be better to write it like this &lt;code&gt;get_age()&lt;/code&gt; but I'm used to methods being in &lt;code&gt;PascalCase&lt;/code&gt; so that's why we wrote it &lt;code&gt;GetAge()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Instance variables
&lt;/h3&gt;

&lt;p&gt;These are just variables but defined inside of a class. Instance variables are available only to each instance of the class. &lt;/p&gt;

&lt;p&gt;Add the following to our Dog class:&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;Dog&lt;/span&gt;

  &lt;span class="c1"&gt;# return the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@age&lt;/code&gt; is an instance variable, so instead of saying for example &lt;code&gt;age = 4&lt;/code&gt; we have to add the &lt;code&gt;@&lt;/code&gt; symbol &lt;code&gt;@age = 4&lt;/code&gt;. That's the only difference in terms of syntax.&lt;/p&gt;

&lt;p&gt;Also here we typed &lt;code&gt;return @age&lt;/code&gt;, this is how we &lt;em&gt;return a value&lt;/em&gt; from a method and it's the same with a lot of other programming languages. In ruby though we can omit the keyword &lt;code&gt;return&lt;/code&gt; and just type &lt;code&gt;@age&lt;/code&gt;. That's because in ruby every method returns the result of the last expression, in our case it will just return the value of &lt;code&gt;@age&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But I like to always know what a method returns as I go through someone's code (even mine), so I like to explicitly return a value from a method.&lt;/p&gt;

&lt;p&gt;Lastly add the following method to our Dog class:&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;Dog&lt;/span&gt;

  &lt;span class="c1"&gt;# return the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# set the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;SetAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Defining an object of class Dog
&lt;/h3&gt;

&lt;p&gt;Add the following lines of code after the Dog class:&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;my_dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Dog&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="c1"&gt;# create a new object of class Dog&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;my_dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;    &lt;span class="c1"&gt;# print the class of the my_dog object&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;my_dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# print the age of the dog before setting it&lt;/span&gt;
&lt;span class="n"&gt;my_dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SetAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;my_dog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="no"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# print the age of the dog after setting it&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Special initialize method
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;initialize&lt;/code&gt; method is a special type method that we can define in every class. What it does it's pretty simple, if we have it defined it will execute as soon as we create a new instance (object) of the class. To test it let's define it in our Dog class:&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;Dog&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="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# return the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;GetAge&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="vi"&gt;@age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="c1"&gt;# set the age of the dog&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;SetAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run our program again and pay attention that the first &lt;code&gt;puts my_dog.GetAge()&lt;/code&gt; instead of printing nothing, it now prints the value &lt;code&gt;3&lt;/code&gt; cause when we did &lt;code&gt;my_dog = Dog.new()&lt;/code&gt; we basically created a new object and it immediately called the &lt;code&gt;initialize&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;We will end Day #3 here but we haven't learnt everything about OOP in Ruby that's because this blog became a bit lengthy so I decided to break it into two parts. So next time we will continue with OOP in Ruby and learn more things such as inheritance.&lt;/p&gt;

&lt;p&gt;Thanks for joining, I will see you next time!&lt;/p&gt;

</description>
      <category>100daysofcode</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>100 Days of Learning ruby - Day #2</title>
      <dc:creator>0x-2FA</dc:creator>
      <pubDate>Sat, 01 Jan 2022 18:39:05 +0000</pubDate>
      <link>https://dev.to/0x2fa/100-days-of-learning-ruby-day-2-4idd</link>
      <guid>https://dev.to/0x2fa/100-days-of-learning-ruby-day-2-4idd</guid>
      <description>&lt;h2&gt;
  
  
  Day 2: Basics of ruby
&lt;/h2&gt;

&lt;p&gt;We are continuing our journey of learning ruby and today we are going to learn the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic data types&lt;/li&gt;
&lt;li&gt;Single and Multi line Comments&lt;/li&gt;
&lt;li&gt;Using methods&lt;/li&gt;
&lt;li&gt;Basic math operations&lt;/li&gt;
&lt;li&gt;Fun with Strings&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setup for Today
&lt;/h3&gt;

&lt;p&gt;Create a new folder called &lt;strong&gt;Day_2&lt;/strong&gt;, then create a new file with the name &lt;strong&gt;basics.rb&lt;/strong&gt; and finally open it up in a plain text editor of your choice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;100_days_of_ruby/
&lt;span class="nb"&gt;mkdir &lt;/span&gt;Day_2 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;Day_2/ &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;touch &lt;/span&gt;basics.rb
codium &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Basic Data Types
&lt;/h3&gt;

&lt;p&gt;Type the following to your text editor:&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="c1"&gt;# Assigning values to variables&lt;/span&gt;

&lt;span class="n"&gt;apples&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;# Integer&lt;/span&gt;
&lt;span class="n"&gt;hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &lt;span class="c1"&gt;# String&lt;/span&gt;
&lt;span class="n"&gt;money&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;6.5&lt;/span&gt; &lt;span class="c1"&gt;# Float&lt;/span&gt;
&lt;span class="n"&gt;is_adult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="c1"&gt;# Boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In ruby you don't have to define the type of a variable before being able to assign a value to it. For example in &lt;strong&gt;C++&lt;/strong&gt; you would say &lt;code&gt;int apples = 4&lt;/code&gt; in ruby though you can simply assign any value to any variable that you want.&lt;/p&gt;

&lt;p&gt;Ruby as you can see has all the basic data types that you see in other languages too. We can have &lt;strong&gt;Integers&lt;/strong&gt;, &lt;strong&gt;Strings&lt;/strong&gt;, &lt;strong&gt;Floats&lt;/strong&gt; and &lt;strong&gt;Booleans&lt;/strong&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Single and Multi line Comments
&lt;/h3&gt;

&lt;p&gt;If you noticed the first line of our file starts with &lt;code&gt;#&lt;/code&gt;, in ruby this is how you create a comment. &lt;/p&gt;

&lt;p&gt;Comments are super useful for us developers that are going to look the code of an app. If you are building a new app and you want other people to contribute to your project, leaving comments to explain how something works is a very good habit to build. It will help others understand what you wrote but also it will help you too, if for example you haven't looked at a piece of code in a long time it can be hard to remember what exactly you were doing.&lt;/p&gt;

&lt;p&gt;So comments are always useful and you should use them when you think that a piece of code needs some sort explanation.&lt;/p&gt;

&lt;p&gt;There are two types of comments in ruby:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Single line comments
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# I'm a single line comment. Everything we type after the character # is a comment.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Multi line comments
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="cm"&gt;=begin
I'm a multi line comment.
We can type anywhere in between =begin and =end

And we can have as many lines as we want! 
=end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use single line comments when we want to give a sort explanation to a line of code. And we use multi line comments when we want to explain something in depth (don't over do it though).&lt;/p&gt;

&lt;h3&gt;
  
  
  Using methods
&lt;/h3&gt;

&lt;p&gt;We won't go over what methods are in depth today but we will need them. So for now methods provide a useful functionality that you can call on an object. Don't worry about it too much and add the following lines of code to our program.&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;puts&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;apples&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_a?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now run the program by typing &lt;code&gt;ruby basics.rb&lt;/code&gt;. The output should be the following:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;We have assigned &lt;code&gt;apples = 4&lt;/code&gt; so the variable &lt;code&gt;apples&lt;/code&gt; is an Integer. Now we called the method &lt;code&gt;is_a?&lt;/code&gt; to check what type is the variable &lt;code&gt;apples&lt;/code&gt;. Obviously when we say &lt;code&gt;apples.is_a?(Integer)&lt;/code&gt; it returns &lt;strong&gt;true&lt;/strong&gt; since &lt;code&gt;apples&lt;/code&gt; has the value 4 and returns &lt;strong&gt;false&lt;/strong&gt; when we check if it's a String.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic math operations
&lt;/h3&gt;

&lt;p&gt;Add the following to our program:&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="c1"&gt;# Basic math operations&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;5&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c1"&gt;# Addition&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c1"&gt;# Subtraction&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="c1"&gt;# Multiplication&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="c1"&gt;# Division&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="c1"&gt;# Modulo&lt;/span&gt;

&lt;span class="nb"&gt;puts&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;2&lt;/span&gt; &lt;span class="c1"&gt;# Exponent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are very self explanatory so I won't go over them. What I wanna highlight though is that when we are calculating a Division or a Modulo operation we always want one of our variables to be of type Float.&lt;/p&gt;

&lt;p&gt;Here &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are both Integers so when do &lt;code&gt;y / x&lt;/code&gt; then answer will always be an Integer. Sometimes that's not a problem, for example if we had &lt;code&gt;6 / 3&lt;/code&gt; that would be equal to &lt;code&gt;2&lt;/code&gt; which is an Integer. But in our case we have &lt;code&gt;12 / 5&lt;/code&gt; which is equal to ~&lt;code&gt;2.4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our output we can see that the result of &lt;code&gt;y / x&lt;/code&gt; is &lt;code&gt;2&lt;/code&gt; which is wrong. To fix that we need to simply make either &lt;code&gt;x&lt;/code&gt; or &lt;code&gt;y&lt;/code&gt; a Float.&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;5.0&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it again and you will notice that now the output is &lt;code&gt;2.4&lt;/code&gt; as it should be.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fun with Strings
&lt;/h3&gt;

&lt;p&gt;As we know already a string is everything that's between &lt;code&gt;" "&lt;/code&gt;. But there are a lot of things that we can do with Strings, we can manipulate them in a lot of ways.&lt;/p&gt;

&lt;p&gt;Add the following to our program:&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="c1"&gt;# Fun with Strings&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello,"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;"World!"&lt;/span&gt; &lt;span class="c1"&gt;# Concatenating strings&lt;/span&gt;

&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt; &lt;span class="c1"&gt;# age is an Integer&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old!"&lt;/span&gt; &lt;span class="c1"&gt;# Interpolating&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break everything down. In the first line we are concatenating three strings together. It might be weird at first that we are using the &lt;code&gt;+&lt;/code&gt; plus symbol for concatenating strings but that's common with many languages.&lt;/p&gt;

&lt;p&gt;Later we define a variable called &lt;code&gt;age&lt;/code&gt; and assign it the value &lt;code&gt;22&lt;/code&gt;. But on the next line we did something weird, we used the &lt;code&gt;#&lt;/code&gt; symbol inside of a string.&lt;/p&gt;

&lt;p&gt;This is how we do Interpolation in ruby, we basically do &lt;code&gt;#{var}&lt;/code&gt; and that will "embed" the value of a variable to a String.&lt;/p&gt;

&lt;p&gt;We will end day #2 by just playing around with some String methods.&lt;/p&gt;

&lt;p&gt;Add the following to our program:&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;puts&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt; &lt;span class="c1"&gt;# print the string in reverse order&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upcase&lt;/span&gt; &lt;span class="c1"&gt;# print the string with every letter being upercase&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;downcase&lt;/span&gt; &lt;span class="c1"&gt;# print the string with every letter being lowercase&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt; &lt;span class="c1"&gt;# print the length of the string &lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty?&lt;/span&gt; &lt;span class="c1"&gt;# check if the string is empty =&amp;gt; returns false cause it's not the case&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;empty?&lt;/span&gt; &lt;span class="c1"&gt;# check if the string is empty =&amp;gt; returns true cause we have an empty string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have explained every method with a comment even though most of them are self explanatory.&lt;/p&gt;

&lt;p&gt;Tomorrow we will go over OOP in ruby and learn a lot more about how ruby really works.&lt;/p&gt;

&lt;p&gt;Thanks for joining, I will see you tomorrow!&lt;/p&gt;

</description>
      <category>100daysofcode</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>100 Days of Learning ruby - Day #1</title>
      <dc:creator>0x-2FA</dc:creator>
      <pubDate>Fri, 31 Dec 2021 11:45:41 +0000</pubDate>
      <link>https://dev.to/0x2fa/100-days-of-learning-ruby-day-1-374l</link>
      <guid>https://dev.to/0x2fa/100-days-of-learning-ruby-day-1-374l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hi, I wanted to learn ruby and what better way than taking up the challenge of #100daysofcode. To be honest I'm not totally new to ruby, but I'm by no means an expert.&lt;/p&gt;

&lt;p&gt;The plan is to code in ruby everyday for at least 1 hour. At the end of the session I will create a new blog here highlighting everything new I learnt in a tutorial style. This way I'm teaching you what I learnt by explaining every detail, this will only benefit me by forcing me to actually understand what I'm doing instead of just typing things.&lt;/p&gt;

&lt;p&gt;I think that these blog posts will also help you learn ruby with me, so I welcome everyone who wants to learn ruby.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I probably won't be covering Ruby on Rails or any other framework. People often mistake ruby with Ruby on Rails, so I wanted to make this disclaimer.&lt;/p&gt;

&lt;p&gt;With all of that out of the way, let's start learning!&lt;/p&gt;

&lt;h2&gt;
  
  
  Day 1: Up and Running
&lt;/h2&gt;

&lt;p&gt;The first thing we need to do is to install ruby and installing it can be a bit tricky some times. Depending on your operating system the process of installing ruby might be different.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Windows&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To install ruby on a Windows machine it's actually pretty easy thanks to &lt;a href="https://rubyinstaller.org/"&gt;RubyInstaller&lt;/a&gt;. I won't go over how to install ruby using RubyInstaller cause it's really easy, just follow the steps of the installer and you will be good to go.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Gnu/Linux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Installing ruby on a Gnu/Linux machine is a bit harder depending on how much comfortable you are with the terminal of course. As far as I know there is not a one click installer for ruby as RubyInstaller for Windows. On Gnu/Linux you generally have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Package Manager&lt;/p&gt;

&lt;p&gt;Using the package manager of your distribution is generally not advised cause most of the time it doesn't have an up to date version of ruby. So I would avoid using this option even though it's easier.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using a Version Manager&lt;/p&gt;

&lt;p&gt;This option is generally preferred cause not only you can install the latest version of ruby but you can also manage multiple versions of ruby in your system. There are a lot of options and I will highlight the most used ones. We can use &lt;strong&gt;&lt;a href="https://rvm.io/"&gt;rvm&lt;/a&gt;&lt;/strong&gt; (ruby version manager), &lt;strong&gt;&lt;a href="https://github.com/rbenv/rbenv"&gt;rbenv&lt;/a&gt;&lt;/strong&gt; with ruby-build and finally &lt;strong&gt;&lt;a href="https://github.com/postmodern/ruby-install#readme"&gt;ruby-install&lt;/a&gt;&lt;/strong&gt; with chruby.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installing rvm
&lt;/h3&gt;

&lt;p&gt;I'm going to use &lt;strong&gt;rvm&lt;/strong&gt; for installing and managing ruby on my machine. I'm not going to go over how to install rvm since they have a really nice guide over on their website. &lt;/p&gt;

&lt;p&gt;Just follow the instructions &lt;a href="https://rvm.io/rvm/install"&gt;here&lt;/a&gt; and you will have rmv installed on your system in no time.&lt;/p&gt;

&lt;p&gt;Check if you have rvm installed correctly by typing the following on your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rvm &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should have something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rvm 1.29.12-next (master) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installing ruby
&lt;/h3&gt;

&lt;p&gt;Now that we have rvm we can easily install any version of ruby with one command. You can find all of this info on their page &lt;a href="https://rvm.io/rvm/install#try-out-your-new-rvm-installation"&gt;here&lt;/a&gt;.&lt;br&gt;
Type the following command to install ruby 3.0.2 on your system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rvm &lt;span class="nb"&gt;install &lt;/span&gt;3.0.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this is done go ahead and use that version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rvm use 3.0.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I have already installed ruby 3.0.2 on my system and currently the latest version of ruby is 3.1.0 and this won't affect my &lt;em&gt;100 days of code&lt;/em&gt; but if you want the latest version of ruby go with 3.1 instead.&lt;/p&gt;

&lt;p&gt;Now if we did everything correctly we should have ruby 3.0.2 installed on our system, to check this type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ruby &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see the following then you have correctly installed ruby and we are ready to begin learning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hello, World!
&lt;/h3&gt;

&lt;p&gt;We will end day #1 of learning ruby by creating our very first ruby program. As everyone knows your first program when learning a new programming language is always the infamous Hello, World! program.&lt;/p&gt;

&lt;p&gt;So let's begin by creating a new folder called &lt;strong&gt;Day_1&lt;/strong&gt; and then create a new ruby file with the name &lt;strong&gt;hello.rb&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;100_days_of_ruby/
&lt;span class="nb"&gt;mkdir &lt;/span&gt;Day_1 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;Day_1/ &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;touch &lt;/span&gt;hello.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now open &lt;strong&gt;hello.rb&lt;/strong&gt; in a plain text editor of your choice. Type the following and save the file:&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;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello, World!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run this simple ruby program open up a terminal and type the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ruby hello.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see the output &lt;strong&gt;Hello, World!&lt;/strong&gt; in your terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  Explanation
&lt;/h4&gt;

&lt;p&gt;This is a very simple ruby program with not much going. We are basically printing &lt;strong&gt;Hello, World!&lt;/strong&gt; on our screen.&lt;/p&gt;

&lt;p&gt;We do that by using the keyword &lt;code&gt;puts&lt;/code&gt;, which prints a string of characters and also goes to the next line. We provided a string of characters by typing Hello, World! inside of &lt;code&gt;" "&lt;/code&gt;. Finally to run the program we typed &lt;code&gt;ruby hello.rb&lt;/code&gt;. Basically when we want to run a ruby program we just type &lt;code&gt;ruby&lt;/code&gt; and the path to our &lt;code&gt;.rb&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Thanks for joining, I will see you tomorrow!&lt;/p&gt;

</description>
      <category>100daysofcode</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
