<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Debug Duck]]></title><description><![CDATA[Debug Duck]]></description><link>https://debugduck.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1653669044805/TuGSDa-oH.png</url><title>Debug Duck</title><link>https://debugduck.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Mon, 18 May 2026 14:40:27 GMT</lastBuildDate><atom:link href="https://debugduck.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Exploring The Well-Grounded Rubyist, Third Edition: Part 1]]></title><description><![CDATA[About the book

The book The Well-Grounded Rubyist, Third Edition was written by David A. Black and Joseph Leo III. Ruby is a general-purpose, object-oriented, interpreted programming language designed by Yukihiro “Matz” Matsumoto. Ruby was first ann...]]></description><link>https://debugduck.dev/exploring-the-well-grounded-rubyist-third-edition-part-1</link><guid isPermaLink="true">https://debugduck.dev/exploring-the-well-grounded-rubyist-third-edition-part-1</guid><category><![CDATA[Ruby]]></category><category><![CDATA[Ruby on Rails]]></category><dc:creator><![CDATA[Kareem Badawy]]></dc:creator><pubDate>Tue, 20 May 2025 17:05:58 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1747760679640/02e491f1-3a15-4ad2-9def-d33e15f728ac.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="heading-about-the-book">About the book</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747664332877/808d0b25-4780-4a71-be99-f3555f126512.png" alt class="image--center mx-auto" /></p>
<p>The book <strong>The Well-Grounded Rubyist, Third Edition</strong> was written by <strong>David A. Black</strong> and <strong>Joseph Leo III</strong>. Ruby is a general-purpose, object-oriented, interpreted programming language designed by Yukihiro “Matz” Matsumoto. Ruby was first announced in 1993. The first public release appeared in 1995, and the language became very popular in Japan during the 1990s.</p>
<p>The purpose of The Well-Grounded Rubyist is to give you a broad and deep understanding of how Ruby works and a considerable toolkit of Ruby techniques and idioms that you can use for real programming, and many other topics, as we will discuss in the following sections.</p>
<p>This edition targets Ruby 2.5.</p>
<p>You can find the book on:</p>
<ul>
<li><p><strong>Manning:</strong> <a target="_blank" href="https://www.manning.com/books/the-well-grounded-rubyist-third-edition">The Well-Grounded Rubyist, Third Edition</a></p>
</li>
<li><p><strong>Amazon:</strong> <a target="_blank" href="https://www.amazon.com/dp/1617295213/">Buy on Amazon</a></p>
</li>
</ul>
<hr />
<h1 id="heading-introduction">Introduction</h1>
<p>I'm currently reading <em>The Well-Grounded Rubyist</em>, and this article is my attempt to share what I've been learning and understanding along the way.</p>
<p>This isn’t meant to replace the book—far from it. I highly recommend that you <strong>buy the book and read it</strong> for a deeper and more complete experience. But if you don’t have enough time right now, I hope this article serves as a helpful starting point—or even motivates you to dive into the book yourself.</p>
<p>And just to be clear:<br />We're talking about <strong>Ruby the programming language</strong>, not <em>Ruby the singer</em>—so no need to wonder, <em>"Leih b</em>eydary <em>keda?"</em> 😄</p>
<hr />
<h1 id="heading-chapter-1-bootstrapping-your-ruby-literacy">Chapter 1 - Bootstrapping your Ruby literacy</h1>
<p>The goal of the chapter is to bootstrap you into the study of Ruby with enough knowledge and skill to proceed comfortably into what lies beyond.</p>
<h2 id="heading-basic-ruby-language-literacy">Basic Ruby language literacy</h2>
<h3 id="heading-installing-ruby-and-using-a-text-editor">Installing Ruby and using a text editor</h3>
<p>Though you’re free to install and compile Ruby from source from <a target="_blank" href="http://www.ruby-lang.org">www.ruby-lang.org</a>, it’s far more common for Rubyists using macOS or Linux to install versions of Ruby using a version manager.</p>
<p>The most popular version managers are <strong>RVM</strong> (<a target="_blank" href="https://rvm.io">https://rvm.io</a>), <strong>rbenv</strong> <em><mark>(personal preference)</mark></em> (<a target="_blank" href="https://github.com/rbenv/rbenv">https://github.com/rbenv/rbenv</a>), and <strong>chruby</strong> (<a target="_blank" href="https://github.com/postmodern/chruby">https://github.com/postmodern/chruby</a>).</p>
<p>Windows users are encouraged to use the <strong>RubyInstaller</strong> (<a target="_blank" href="https://rubyinstaller.org/">https://rubyinstaller.org/</a>).</p>
<p>All version managers are free, and all provide a safe and easy way to download and run Ruby. This book references <strong>Ruby version 2.5.1</strong>.</p>
<p>You’ll also need a text editor (any editor you like, as long as it’s a plain-text editor and not a word processor like <a target="_blank" href="https://code.visualstudio.com/">VSCode</a> <em><mark>(personal preference)</mark></em> and <a target="_blank" href="https://www.jetbrains.com/ruby/">RubyMine</a> [IDE]) and a directory (a.k.a. a folder) in which to store your Ruby program files.</p>
<p><strong>The interactive Ruby console program (irb)</strong>, your new best friend: The irb utility ships with Ruby and is the most widely used Ruby command-line tool, aside from the interpreter itself.</p>
<p>After starting irb, type Ruby code into it, and the code executes, printing out the resulting value.</p>
<p>Type <code>irb</code> at the command line and enter sample code as you encounter it in the text.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747646439959/96f6afc1-51ab-49f9-9b4a-689d95138576.png" alt class="image--center mx-auto" /></p>
<p>To exit from <code>irb</code> normally, you can type <code>exit</code>. On many systems, Ctrl+D works too.</p>
<p>If you find <code>irb(main):001&gt;</code> is too noisy, you can use <code>irb --simple-prompt</code>, which makes irb output easier to read.</p>
<hr />
<h3 id="heading-a-ruby-syntax-survival-kit">A Ruby syntax survival kit</h3>
<blockquote>
<p>In this section, we’ll start exploring some of the basic elements of Ruby syntax. You’ll learn:</p>
<ul>
<li><p>How to perform arithmetic operations</p>
</li>
<li><p>How to store objects in variables so you can reuse them later in your code</p>
</li>
<li><p>How to compare object values using equality and comparison operators</p>
</li>
<li><p>And how to write conditional logic by comparing variables with each other</p>
</li>
</ul>
<p>These are the building blocks of writing meaningful Ruby code, and they’ll give you a solid foundation to build on as we go deeper into the language.</p>
</blockquote>
<p><strong>Basic operations in Ruby:</strong></p>
<ol>
<li><p><strong>Arithmetic operations:</strong></p>
<ol>
<li><p>Addition: <code>1 + 2</code></p>
</li>
<li><p>Subtraction: <code>4 - 3</code></p>
</li>
<li><p>Multiplication: <code>3 * 7</code></p>
</li>
<li><p>Division: <code>1 / 3</code></p>
<p> All these operations work on integers or floating-point numbers (floats). Mixing integers and floats together, as some of the examples do, produces a floating-point result.</p>
<blockquote>
<p><strong><em>What is a Float?</em></strong></p>
<p><em>In Ruby, a</em> <strong><em>float</em></strong> <em>(short for floating-point number) is a number that has a decimal point. It’s used to represent real numbers, including both whole and fractional parts.</em></p>
<p><em>For example:</em></p>
<pre><code class="lang-ruby"><span class="hljs-number">3.14</span>    <span class="hljs-comment"># a float</span>
<span class="hljs-number">0</span>.<span class="hljs-number">5</span>     <span class="hljs-comment"># a float</span>
<span class="hljs-number">10.0</span>    <span class="hljs-comment"># also a float, even though it's a whole number</span>
</code></pre>
<p><em>You’ll often encounter floats in Ruby when:</em></p>
<ul>
<li><p><em>Performing division that results in a non-integer value (e.g.,</em> <code>5 / 2.0</code> <em>gives</em> <code>2.5</code><em>)</em></p>
</li>
<li><p><em>Working with measurements, prices, or anything that requires precision beyond whole numbers</em></p>
</li>
</ul>
<p><em>💡</em> <strong><em>Note:</em></strong> <em>If you divide two integers in Ruby (like</em> <code>5 / 2</code><em>), the result is also an integer (</em><code>2</code><em>). To get a float result, at least one number should be a float (</em><code>5.0 / 2</code> <em>or</em> <code>5 / 2.0</code><em>).</em></p>
</blockquote>
</li>
</ol>
</li>
<li><p><strong>Assignment operations in Ruby:</strong></p>
<ol>
<li><p><code>x = 1</code></p>
</li>
<li><p><code>string = "Hello World!"</code></p>
<blockquote>
<p><em>This operation binds a</em> <strong><em>local variable</em></strong> <em>(on the left) to an</em> <strong><em>object</em></strong> <em>(on the right). In Ruby, everything is an object, including numbers, strings, and even</em> <code>nil</code> (a.k.a. nothing)<em>.</em></p>
<ul>
<li><p><code>x = 1</code> <em>assigns the integer object</em> <code>1</code> <em>to the variable</em> <code>x</code><em>.</em></p>
</li>
<li><p><code>string = "Hello World!"</code> <em>assigns the string object</em> <code>"Hello World!"</code> <em>to the variable</em> <code>string</code><em>.</em></p>
</li>
</ul>
<p><em>💡</em> <strong><em>Note:</em></strong> <em>Variables in Ruby don’t have a fixed type—they simply refer to an object. That means you can reassign</em> <code>x</code> <em>to a different kind of value later:</em></p>
<pre><code class="lang-ruby">x = <span class="hljs-number">1</span>
x = <span class="hljs-string">"Now I'm a string"</span>
</code></pre>
<p><em>This dynamic behavior makes Ruby very flexible, but it also means you should keep track of what your variables represent to avoid confusion.</em></p>
</blockquote>
<p> <strong>Working with Strings in Ruby</strong></p>
<blockquote>
<p>In Ruby, a <strong>string</strong> is a sequence of characters enclosed in quotes. You can use either <strong>single quotes</strong> <code>'</code> or <strong>double quotes</strong> <code>"</code> to create strings.</p>
<p><strong>Basic Examples:</strong></p>
<pre><code class="lang-ruby">greeting = <span class="hljs-string">"Hello, world!"</span>
name = <span class="hljs-string">'Ruby'</span>
</code></pre>
<p><strong>Difference Between Single and Double Quotes:</strong></p>
<p><strong>Single quotes</strong> <code>'...'</code> <strong>treat the content literally.</strong></p>
<ul>
<li><strong>Double quotes</strong> <code>"..."</code> allow <strong>string interpolation</strong> and <strong>escape sequences</strong>.</li>
</ul>
<p><strong>Interpolation (Only with</strong> <code>"</code><strong>):</strong></p>
<p>You can insert variable values into a string using <code>#{...}</code>:</p>
<pre><code class="lang-ruby">name = <span class="hljs-string">"Kareem"</span>
puts <span class="hljs-string">"Hello, <span class="hljs-subst">#{name}</span>!"</span>   <span class="hljs-comment"># =&gt; Hello, Kareem!</span>
</code></pre>
<p>With single quotes, interpolation won't work:</p>
<pre><code class="lang-ruby">puts <span class="hljs-string">'Hello, <span class="hljs-subst">#{name}</span>!'</span>   <span class="hljs-comment"># =&gt; Hello, #{name}!</span>
</code></pre>
<p><strong>Common String Methods:</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747737238499/3d241e65-6f3b-441e-9824-adcd140348c1.png" alt class="image--center mx-auto" /></p>
<p><strong>Concatenation:</strong></p>
<p>You can combine strings using <code>+</code>:</p>
<pre><code class="lang-ruby">first = <span class="hljs-string">"Hello"</span>
second = <span class="hljs-string">"World"</span>
puts first + <span class="hljs-string">" "</span> + second  <span class="hljs-comment"># =&gt; Hello World</span>
</code></pre>
<p>Or use interpolation:</p>
<pre><code class="lang-ruby">puts <span class="hljs-string">"<span class="hljs-subst">#{first}</span> <span class="hljs-subst">#{second}</span>"</span>  <span class="hljs-comment"># =&gt; Hello World</span>
</code></pre>
<p><strong>Accessing Characters:</strong></p>
<p>Strings are like arrays of characters. You can use indices:</p>
<pre><code class="lang-ruby">greeting = <span class="hljs-string">"Hello"</span>
puts greeting[<span class="hljs-number">0</span>]  <span class="hljs-comment"># =&gt; "H"</span>
puts greeting[-<span class="hljs-number">1</span>] <span class="hljs-comment"># =&gt; "o" (last character)</span>
</code></pre>
<p><strong>Summary</strong></p>
<p>Strings are one of the most commonly used data types in Ruby. They are flexible, powerful, and come with many built-in methods that make text manipulation easy and expressive.</p>
</blockquote>
</li>
</ol>
</li>
<li><p><strong>Comparing values in Ruby:</strong></p>
<ul>
<li><p>To compare two values for equality, Ruby uses the <strong>double equals sign (</strong><code>==</code>):</p>
<pre><code class="lang-ruby">  x == y
</code></pre>
<blockquote>
<p><em>This checks whether the value of</em> <code>x</code> <em>is</em> <strong><em>equal to</em></strong> <em>the value of</em> <code>y</code><em>.</em></p>
<p><em>⚠️</em> <strong><em>Important:</em></strong> <em>Don't confuse this with a single equals sign (</em><code>=</code><em>), which is used for</em> <strong><em>assignment</em></strong> <em>(e.g.,</em> <code>x = y</code> <em>assigns</em> <code>y</code> <em>to</em> <code>x</code><em>).</em></p>
<p>Example:</p>
<pre><code class="lang-ruby">x = <span class="hljs-number">5</span>
y = <span class="hljs-number">5</span>
x == y   <span class="hljs-comment"># =&gt; true</span>

x = <span class="hljs-number">5</span>
y = <span class="hljs-number">6</span>
x == y   <span class="hljs-comment"># =&gt; false</span>
</code></pre>
<p><em>You can also use other comparison operators:</em></p>
<ul>
<li><p><code>!=</code> <em>— not equal</em></p>
</li>
<li><p><code>&lt;</code> <em>— less than</em></p>
</li>
<li><p><code>&gt;</code> <em>— greater than</em></p>
</li>
<li><p><code>&lt;=</code> <em>— less than or equal to</em></p>
</li>
<li><p><code>&gt;=</code> <em>— greater than or equal to</em></p>
</li>
</ul>
<p><em>These are essential for making decisions in your code.</em></p>
</blockquote>
</li>
</ul>
</li>
<li><p><strong>Convert a numeric string to a number:</strong></p>
<ol>
<li><p><code>x = "100".to_i</code></p>
</li>
<li><p><code>s = "100"</code></p>
</li>
<li><p><code>x = s.to_i</code></p>
<p> To perform arithmetic, you have to make sure you have numbers rather than strings of characters. to_i performs string-to-integer conversion.</p>
</li>
</ol>
</li>
</ol>
<hr />
<p><strong>Basic input/output methods and flow control in Ruby:</strong></p>
<ol>
<li><p><strong>Print something to the screen:</strong></p>
<ul>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747650574972/258f2ea0-45f2-4aa2-9a66-a7b68e2502de.png" alt class="image--center mx-auto" /></p>
<p>  <code>puts</code> adds a newline to the string it outputs if there isn’t one at the end already; <code>print</code> doesn’t.</p>
<p>  <code>print</code> prints exactly what it’s told to and leaves the cursor at the end. (Note: On some platforms, an extra line is automatically output at the end of a program.)</p>
<p>  <code>p</code> outputs an inspect string, which may contain extra information about what it’s printing.</p>
</li>
</ul>
</li>
<li><p><strong>Get a line of keyboard input:</strong></p>
<ul>
<li><p><code>gets</code></p>
</li>
<li><p><code>string = gets</code></p>
<p>  You can assign the input line directly to a variable (the variable string in the second example). Try gets in an <code>irb</code> session, and a cursor will wait for you to enter input.</p>
</li>
</ul>
</li>
</ol>
<hr />
<p><strong>Conditional execution:</strong></p>
<ul>
<li><pre><code class="lang-ruby">  x = <span class="hljs-number">1</span>
  y = <span class="hljs-number">1</span>
  <span class="hljs-keyword">if</span> x == y
   puts <span class="hljs-string">"Yes!"</span>
  <span class="hljs-keyword">else</span>
   puts <span class="hljs-string">"No!"</span>
  <span class="hljs-keyword">end</span>
</code></pre>
<blockquote>
<p><em>In Ruby, you can run different blocks of code depending on whether a condition is true or false.</em></p>
<ul>
<li><p><em>The</em> <code>if</code> <em>keyword starts a condition.</em></p>
</li>
<li><p><code>x == y</code> <em>checks whether</em> <code>x</code> <em>is equal to</em> <code>y</code><em>.</em></p>
</li>
<li><p><code>puts</code> <em>prints a line to the console.</em></p>
</li>
<li><p><em>The</em> <code>else</code> <em>block runs if the condition is false.</em></p>
</li>
<li><p><em>The statement ends with</em> <code>end</code><em>, which is required.</em></p>
</li>
</ul>
<p>Output:</p>
<p><em>Since</em> <code>x</code> <em>and</em> <code>y</code> <em>are both</em> <code>1</code><em>, this code will print:</em></p>
<pre><code class="lang-ruby">Yes!
</code></pre>
<p><em>You can also use</em> <code>elsif</code> <em>to add more conditions. You'll learn more about that in Chapter 6.</em></p>
</blockquote>
</li>
</ul>
<hr />
<p><strong>Ruby’s special objects and comments:</strong></p>
<ol>
<li><p><strong>Special value objects:</strong></p>
<ol>
<li><p><code>true</code></p>
</li>
<li><p><code>false</code></p>
</li>
<li><p><code>nil</code></p>
<p> The objects <code>true</code> and <code>false</code> often serve as return values for conditional expressions. The object <code>nil</code> is a kind of “nonobject” indicating the absence of a value or result. <code>false</code> and <code>nil</code> cause a conditional expression to evaluate as false; all other objects (including <code>true</code>, of course, but also including 0 and empty strings) cause it to evaluate to true. More on these in chapter 7.</p>
</li>
</ol>
</li>
<li><p><strong>Default object:</strong></p>
<ul>
<li><p><code>self</code></p>
<p>  The keyword <code>self</code> refers to the default object. Self is a role that different objects play, depending on the execution context. Method calls that don’t specify a calling object are called on self. More on this in chapter 5.</p>
</li>
</ul>
</li>
<li><p><strong>Put comments in code files:</strong></p>
<ul>
<li><pre><code class="lang-ruby">  <span class="hljs-comment"># A comment</span>
  x = <span class="hljs-number">1</span> <span class="hljs-comment"># A comment</span>
</code></pre>
<p>  Comments are ignored by the interpreter.</p>
</li>
</ul>
</li>
</ol>
<hr />
<h3 id="heading-the-variety-of-ruby-identifiers">The variety of Ruby identifiers</h3>
<p>Ruby has a small number of identifier types that you’ll want to be able to spot and differentiate from each other at a glance. The identifier family tree looks like this:</p>
<ul>
<li><p>Variables:</p>
<ul>
<li><p>Local</p>
</li>
<li><p>Instance</p>
</li>
<li><p>Class</p>
</li>
<li><p>Global</p>
</li>
</ul>
</li>
<li><p>Constants</p>
</li>
<li><p>Keywords</p>
</li>
<li><p>Method names</p>
</li>
</ul>
<p><strong>VARIABLES:</strong></p>
<p><strong>Local variables:</strong> start with a lowercase letter or an underscore and consist of letters, underscores, and/or digits. <code>x</code>, <code>string</code>, <code>abc</code>, <code>var1</code>, <code>start_value</code>, and <code>firstName</code> are all valid local variable names.</p>
<p>💡Note, however, that the Ruby convention is to use underscores rather than camel case when composing local variable names from multiple words—for example, <code>first_name</code> rather than <code>firstName</code>.</p>
<p><strong>Instance variables:</strong> which serve the purpose of storing information within individual objects, always start with a single at-sign (@) and consist thereafter of the same character set as local variables—for example, <code>@age</code> and <code>@last_name</code>.</p>
<p><strong>Class variables:</strong> which store information per class hierarchy (again, don’t worry about the semantics at this stage), follow the same rules as instance variables, except that they start with two at-signs—for example, <code>@@running_total</code>.</p>
<p><strong>Global variables:</strong> are recognizable by their leading dollar sign ($)—for example, <code>$population</code>. The segment after the dollar sign doesn’t follow local-variable naming conventions; there are global variables called <code>$:</code>, <code>$1</code>, and <code>$/</code>, as well as <code>$stdin</code> and <code>$LOAD_PATH</code>. As long as it begins with a dollar sign, it’s a global variable.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Type</strong></td><td><strong>Ruby convention</strong></td><td><strong>Nonconventional</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>Local</strong></td><td><code>first_name</code></td><td><code>firstName</code>, <code>firstName</code><em>,</em> <code>_firstName</code>, <code>name1</code></td></tr>
<tr>
<td><strong>Instance</strong></td><td><code>@first_name</code></td><td><code>@First_name</code>, <code>@firstName</code></td></tr>
<tr>
<td><strong>Class</strong></td><td><code>@@first_name</code></td><td><code>@@First_name</code>, <code>@@firstName</code></td></tr>
<tr>
<td><strong>Global</strong></td><td><code>$FIRST_NAME</code></td><td><code>$first_name</code>, <code>$firstName</code>, <code>$name1</code></td></tr>
</tbody>
</table>
</div><hr />
<p><strong>CONSTANTS:</strong></p>
<p>Constants begin with an uppercase letter. A, String, <code>FirstName</code>, and <code>STDIN</code> are all valid constant names. The Ruby convention is to use either camel case (<code>FirstName</code>) or underscore-separated all-uppercase words (<code>FIRST_NAME</code>) in composing constant names from multiple words.</p>
<hr />
<p><strong>KEYWORDS:</strong></p>
<p>Ruby has numerous keywords—predefined, reserved terms associated with specific programming tasks and contexts. Keywords include <code>def</code> (for method definitions), <code>class</code> (for class definitions), <code>if</code> (conditional execution), and <code>ــFILE__</code> (the name of the file currently being executed). There are about 40 of them, and they’re generally short, single-word (as opposed to underscore-composed) identifiers.</p>
<p>You can see them here: <a target="_blank" href="https://docs.ruby-lang.org/en/2.5.0/keywords_rdoc.html">https://docs.ruby-lang.org/en/2.5.0/keywords_rdoc.html</a></p>
<hr />
<p><strong>METHOD NAMES:</strong></p>
<p>Names of methods in Ruby follow the same rules and conventions as local variables (except that they can end with ?, !, or =, with significance that you’ll see later).</p>
<hr />
<h3 id="heading-method-calls-messages-and-ruby-objects">Method calls, messages, and Ruby objects</h3>
<blockquote>
<p><strong>Introduction to Functions in Ruby</strong></p>
<p>As your Ruby programs grow, you'll need ways to organize your code and avoid repeating yourself. That’s where functions and classes come in.</p>
<p>What is a Function (Method) in Ruby?</p>
<p>A function (called a method in Ruby) is a reusable block of code that performs a specific task. You define it once and call it whenever you need it.</p>
<p>Example:</p>
<pre><code class="lang-ruby"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">greet</span><span class="hljs-params">(name)</span></span>
  puts <span class="hljs-string">"Hello, <span class="hljs-subst">#{name}</span>!"</span>
<span class="hljs-keyword">end</span>

greet(<span class="hljs-string">"Ruby"</span>)  <span class="hljs-comment"># =&gt; Hello, Ruby!</span>
</code></pre>
<ul>
<li><p><code>def</code> starts the method definition.</p>
</li>
<li><p><code>name</code> is a parameter.</p>
</li>
<li><p>You call it using the method name and pass an argument.</p>
</li>
</ul>
<p>Ruby has many built-in methods, and you can also write your own to make your code cleaner and more modular.</p>
<p>Ruby sees all data structures and values—from simple scalar (atomic) values like integers and strings, to complex data structures like arrays—as <strong>objects</strong>.</p>
</blockquote>
<p>In Ruby, <strong>every object can respond to a set of messages</strong>. These messages correspond to <strong>methods</strong>—named actions that the object knows how to perform.</p>
<blockquote>
<p><strong>Method calls as messages</strong></p>
<p>A <strong>method</strong> is like a command or request you send to an object. When you call a method, you're sending a <strong>message</strong> to an object asking it to do something or give you information.</p>
<p><strong>Using the dot (.) operator</strong></p>
<p>The most common way to send a message (i.e., call a method) is using the <strong>dot operator (</strong><code>.</code>):</p>
<pre><code class="lang-ruby"><span class="hljs-string">"100"</span>.to_i
</code></pre>
<p>In this line:</p>
<ul>
<li><p><code>"100"</code> is a <strong>string object</strong> and the <strong>receiver</strong> of the message.</p>
</li>
<li><p><code>to_i</code> is the <strong>method</strong> (or message) sent to the string.</p>
</li>
<li><p>The result is the integer <code>100</code>, because <code>to_i</code> converts a string to an integer.</p>
</li>
</ul>
<p><strong>Storing the result</strong></p>
<p>You can assign the result of a method call to a variable:</p>
<pre><code class="lang-ruby">x = <span class="hljs-string">"100"</span>.to_i
<span class="hljs-comment"># x now holds the integer 100</span>
</code></pre>
<p>This pattern is common in Ruby:<br /><strong>object.method → result</strong></p>
<p>So, <code>"hello".upcase</code> sends the <code>upcase</code> message to the string <code>"hello"</code>, which returns <code>"HELLO"</code>.</p>
</blockquote>
<p><strong>Why the double terminology?</strong></p>
<blockquote>
<p>You may notice that Rubyists sometimes say things like:</p>
<ul>
<li><p>“Calling the <code>to_i</code> method”</p>
</li>
<li><p>or “Sending the <code>to_i</code> message to a string”</p>
</li>
</ul>
<p>Aren’t those the same thing?</p>
<p><strong>Not exactly.</strong></p>
<p>While <strong>calling a method</strong> is the common terminology in most programming languages, Ruby encourages thinking in terms of <strong>sending messages</strong>. Here's why the distinction matters:</p>
<p><strong>Message vs. Method</strong></p>
<ul>
<li><p><strong>Message</strong>: A request sent to an object to perform an action (e.g., <code>"100".to_i</code>)</p>
</li>
<li><p><strong>Method</strong>: The actual implementation the object uses to respond to that message</p>
</li>
</ul>
<p>So, when you write:</p>
<pre><code class="lang-ruby"><span class="hljs-string">"100"</span>.to_i
</code></pre>
<p>You’re <strong>sending the message</strong> <code>to_i</code> to the string <code>"100"</code>. If the string knows how to handle that message (i.e., it has a method named <code>to_i</code>), it responds accordingly.</p>
<p>But what if it doesn’t?</p>
<p><strong>Dynamic behavior with</strong> <code>method_missing</code></p>
<p>Ruby is flexible—if an object doesn’t have a method that matches the message, it doesn't crash right away. Instead, Ruby gives the object a chance to handle the situation using a special method called <code>method_missing</code>.</p>
<p>This is a powerful feature that allows developers to write dynamic code. For example:</p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Ghost</span></span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">method_missing</span><span class="hljs-params">(method_name, *args)</span></span>
    puts <span class="hljs-string">"You tried to call '<span class="hljs-subst">#{method_name}</span>', but I'm a ghost!"</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

phantom = Ghost.new
phantom.disappear  <span class="hljs-comment"># =&gt; You tried to call 'disappear', but I'm a ghost!</span>
</code></pre>
<p>This flexibility is used extensively in frameworks like <strong>Ruby on Rails</strong>, where method names can be constructed dynamically (e.g., <code>find_by_email_and_name</code>) and caught using <code>method_missing</code>.</p>
<p><strong>Summary</strong></p>
<ul>
<li><p>"Calling a method" is familiar and straightforward.</p>
</li>
<li><p>"Sending a message" is conceptually richer—it captures how Ruby lets objects decide <em>how</em> (or <em>if</em>) they respond.</p>
</li>
<li><p>If no method matches, Ruby gives objects one last chance to respond via <code>method_missing</code>.</p>
</li>
</ul>
</blockquote>
<p><strong>Method Calls, Arguments, and the Power of Objects</strong></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747692871484/cce3508c-5b44-4e8d-a14e-67ce8ed2e5fc.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p>In Ruby, <strong>methods can take arguments</strong>, and—like nearly everything else in Ruby—<strong>arguments are also objects</strong>.</p>
<p><strong>Example with an argument:</strong></p>
<pre><code class="lang-ruby">x = <span class="hljs-string">"100"</span>.to_i(<span class="hljs-number">9</span>)
</code></pre>
<ul>
<li><p>Here, we're calling the <code>to_i</code> method <strong>on the string</strong> <code>"100"</code>.</p>
</li>
<li><p>We're passing <code>9</code> as an <strong>argument</strong>, telling Ruby to interpret <code>"100"</code> as a <strong>base-9</strong> number.</p>
</li>
<li><p>The result is the decimal equivalent: <code>81</code>. So <code>x</code> is now <code>81</code>.</p>
</li>
</ul>
<p><strong>Parentheses: optional, but helpful</strong></p>
<p>In Ruby, parentheses around method arguments are <strong>usually optional</strong>:</p>
<pre><code class="lang-ruby"><span class="hljs-string">"100"</span>.to_i <span class="hljs-number">9</span>  <span class="hljs-comment"># works, but may be hard to read</span>
</code></pre>
<p>However, they are recommended for <strong>clarity</strong>, especially when:</p>
<ul>
<li><p>There are multiple arguments</p>
</li>
<li><p>You're chaining methods</p>
</li>
<li><p>The syntax might otherwise be ambiguous</p>
</li>
</ul>
<p>Many Rubyists use parentheses consistently in method calls just to be safe.</p>
<p><strong>Everything is an Object. Messages are Method Calls.</strong></p>
<p>Ruby programs are made up of <strong>objects</strong> and <strong>messages</strong>. As a Ruby developer, your two core activities are:</p>
<ol>
<li><p><strong>Defining methods</strong> (what you want objects to be able to do)</p>
</li>
<li><p><strong>Calling methods</strong> (sending messages to objects to ask them to do something)</p>
</li>
</ol>
<p>The <strong>dot operator (</strong><code>.</code>) connects the message to the object:</p>
<pre><code class="lang-ruby">object.method(arguments)
</code></pre>
<p>Think of it as:<br /><strong>Send the message</strong> <code>method</code> to <code>object</code> with some arguments.</p>
<p><strong>Implicit Receivers: When You Don’t See the Dot</strong></p>
<p>Some method calls don’t show a dot or an object at all. For example:</p>
<pre><code class="lang-ruby">puts <span class="hljs-string">"Hello"</span>
</code></pre>
<p>There’s no visible object receiving <code>puts</code>, but it’s still a <strong>method call</strong>. In this case, the method is being sent to the <strong>default object:</strong> <code>self</code>.</p>
<p>Ruby <strong>always has a current</strong> <code>self</code> during execution, and any method call without an explicit receiver is automatically sent to <code>self</code>.</p>
<p><strong>Objects and Classes</strong></p>
<p>The <strong>most important concept in Ruby is the object</strong>.</p>
<p>Closely connected to it is the idea of the <strong>class</strong>—which defines how objects behave:</p>
<ul>
<li><p>What methods they respond to</p>
</li>
<li><p>How they are constructed</p>
</li>
<li><p>How they interact with other objects</p>
<p>  You’ll dive deeper into classes later, but for now, remember: everything in Ruby revolves around <strong>objects</strong>, <strong>messages</strong>, and the methods those objects use to respond.</p>
</li>
</ul>
</blockquote>
<p><strong>The Origin of Objects: Classes</strong></p>
<blockquote>
<p>In Ruby, <strong>every object belongs to a class</strong>—a blueprint that defines the object’s behavior and available methods.</p>
<p><strong>What is a class?</strong></p>
<p>A <strong>class</strong> defines what an object can do:</p>
<ul>
<li><p>What methods it responds to</p>
</li>
<li><p>How it's initialized</p>
</li>
<li><p>What kind of data it holds</p>
</li>
</ul>
<p>Every object in Ruby is an <strong>instance</strong> of exactly <strong>one class</strong>. For example:</p>
<pre><code class="lang-ruby"><span class="hljs-string">"hello"</span>.<span class="hljs-keyword">class</span>   <span class="hljs-comment"># =&gt; String</span>
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>].<span class="hljs-keyword">class</span> <span class="hljs-comment"># =&gt; Array</span>
<span class="hljs-number">5</span>.<span class="hljs-keyword">class</span>         <span class="hljs-comment"># =&gt; Integer</span>
</code></pre>
<p>When you write <code>"hello"</code>, you’re creating a <strong>String object</strong>—an instance of the <code>String</code> class.</p>
<p><strong>Built-in vs. Custom Classes</strong></p>
<p>Ruby comes with many built-in classes: <code>String</code>, <code>Array</code>, <code>Hash</code>, <code>Integer</code>, <code>Float</code>, and so on.</p>
<p>But you can also define your own classes:</p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span></span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bark</span></span>
    <span class="hljs-string">"Woof!"</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

fido = Dog.new
puts fido.bark  <span class="hljs-comment"># =&gt; "Woof!"</span>
</code></pre>
<p>Here, <code>Dog</code> is a custom class, and <code>fido</code> is an object (instance) of that class.</p>
<p><strong>Modifying Built-in Classes (Monkey Patching)</strong></p>
<p>Ruby is <strong>highly flexible</strong>—you can even change the behavior of built-in classes:</p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">String</span></span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">shout</span></span>
    <span class="hljs-keyword">self</span>.upcase + <span class="hljs-string">"!"</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

puts <span class="hljs-string">"hello"</span>.shout  <span class="hljs-comment"># =&gt; "HELLO!"</span>
</code></pre>
<p>While this is possible, it’s usually <strong>discouraged</strong> in production code because it can lead to <strong>unexpected bugs</strong> or conflicts—especially in large applications or libraries.</p>
<p>⚠️ You’ll explore the risks and use cases of this technique (called <em>monkey patching</em>) in Chapter 13.</p>
</blockquote>
<hr />
<h3 id="heading-writing-and-saving-a-simple-program">Writing and saving a simple program</h3>
<p>At this point, you can start creating program files in the Ruby sample code directory you created a little while back. Your first program will be a Celsius-to-Fahrenheit temperature converter.</p>
<p>The first version will be simple; the focus will be on the file-creation and programrunning processes, rather than any elaborate program logic.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747661386734/9e54b566-87a1-4e5e-a8c3-e1debaed282a.png" alt class="image--center mx-auto" /></p>
<p>Using a plain-text editor, type the code from the following listing into a text file and save it under the filename c2f.rb in your sample code directory.</p>
<p>You now have a complete (albeit tiny) Ruby program on your disk, and you can run it.</p>
<hr />
<h3 id="heading-feeding-the-program-to-ruby">Feeding the program to Ruby</h3>
<p>Running a Ruby program involves passing the program’s source file (or files) to the Ruby interpreter, which is called ruby. You’ll do that now ... sort of. You’ll feed the program to ruby, but instead of asking Ruby to run the program, you’ll ask it to check the program code for syntax errors.</p>
<pre><code class="lang-ruby">$ ruby -cw c2f.rb
</code></pre>
<p>The -cw command-line flag is shorthand for two flags: -c and -w. The -c flag means check for syntax errors. The -w flag activates a higher level of warning: Ruby will fuss at you if you’ve done things that are legal Ruby but are questionable on grounds other than syntax.</p>
<p><strong>RUNNING THE PROGRAM</strong></p>
<pre><code class="lang-ruby">$ ruby c2f.rb
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1747661966087/01cb5d42-fed6-4944-b566-4eb51d290328.png" alt class="image--center mx-auto" /></p>
<p>The result of the calculation is correct, but having the output spread over three lines looks bad.</p>
<p><strong>SECOND CONVERTER ITERATION</strong></p>
<p>The problem can be traced to the difference between the puts command and the print command. puts adds a newline to the end of the string it prints out, if the string doesn’t end with one already. print, on the other hand, prints out the string you ask it to and then stops; it doesn’t automatically jump to the next line. To fix the problem, change the first two puts commands to print:</p>
<pre><code class="lang-ruby">print <span class="hljs-string">"The result is "</span>
print fahrenheit
puts <span class="hljs-string">"."</span>
</code></pre>
<hr />
<h2 id="heading-summary">Summary</h2>
<p>In this chapter, you’ve taken your first steps into the Ruby programming language by learning its foundational concepts:</p>
<ul>
<li><p><strong>Ruby is an object-oriented language</strong>: Everything is an object—even numbers, strings, and <code>nil</code>.</p>
</li>
<li><p><strong>Method calls are message sends</strong>: Ruby encourages you to think of methods as messages sent to objects, using the dot (<code>.</code>) operator.</p>
</li>
<li><p><strong>Variables are flexible</strong>: They hold references to objects, and you can reassign them to different types anytime.</p>
</li>
<li><p><strong>You explored key Ruby syntax</strong>:</p>
<ul>
<li><p>Arithmetic and comparison operations</p>
</li>
<li><p>Input/output with <code>puts</code>, <code>print</code>, <code>p</code>, and <code>gets</code></p>
</li>
<li><p>Conditional execution with <code>if</code>, <code>else</code>, and <code>elsif</code></p>
</li>
</ul>
</li>
<li><p><code>self</code> refers to the current default object: This plays a central role in how methods are called and how code is evaluated.</p>
</li>
<li><p><strong>Ruby classes define object behavior</strong>: Every object is an instance of a class, but Ruby’s flexibility allows objects to be extended beyond their original class.</p>
</li>
<li><p><strong>You wrote and ran your first Ruby program</strong>, learning how to use the interpreter and correct formatting issues using <code>puts</code> and <code>print</code>.</p>
</li>
</ul>
<p>As you continue reading, you’ll go deeper into Ruby’s object model, methods, control structures, and more advanced patterns like dynamic behavior with <code>method_missing</code>. With this foundation, you're now ready to write more powerful and idiomatic Ruby code.</p>
<hr />
<h1 id="heading-final-note">Final Note</h1>
<p>If you enjoyed this article or found it helpful, I’d love to hear your thoughts!</p>
<p>Feel free to share your feedback—especially if you notice anything that needs correction. I’m always happy to improve the content.</p>
<p>Thanks for reading, and stay tuned for the next articles in this series as we continue exploring <em>The Well-Grounded Rubyist</em> together.</p>
]]></content:encoded></item></channel></rss>