<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Bug Bytes - Medium]]></title>
        <description><![CDATA[Quick tips on debugging and refactoring source code. - Medium]]></description>
        <link>https://medium.com/bug-bytes?source=rss----bec57b3080cd---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Bug Bytes - Medium</title>
            <link>https://medium.com/bug-bytes?source=rss----bec57b3080cd---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 16:21:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/bug-bytes" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Global variables across multiple files (Ruby)]]></title>
            <link>https://medium.com/bug-bytes/global-variables-across-multiple-files-ruby-ffefb5f3b886?source=rss----bec57b3080cd---4</link>
            <guid isPermaLink="false">https://medium.com/p/ffefb5f3b886</guid>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Ewe Lin Loo]]></dc:creator>
            <pubDate>Sun, 07 Apr 2019 03:18:06 GMT</pubDate>
            <atom:updated>2019-04-07T03:17:45.583Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fuQ64Go1U-A_KZkxa5rX-Q.png" /></figure><p>Here’s how to separate an array from simple source code to a separate file. This short tutorial was inspired by <a href="https://stackoverflow.com/questions/8288271/how-to-reference-an-array-from-one-ruby-script-to-another">this</a> Stack Overflow post. Note: This method is only recommended for short pieces of code.</p><p>We’re starting off with the code below. Here you can see that items is an array of hashes.</p><pre>items = [{ customer: &quot;John&quot;, item: &quot;Soup&quot;, cost: 8.50}, {customer: &quot;Sarah&quot;, item: &quot;Pasta&quot;, cost: 12}, {customer: &quot;John&quot;, item: &quot;Coke&quot;, cost: 2.50}]</pre><pre>puts &quot;Enter a customer&#39;s name.&quot;</pre><pre>customer_owing = gets.chomp.capitalize</pre><pre>owed = 0</pre><pre># Cycle through array of hashes</pre><pre>array_elem = items.each do |hash|</pre><p>The first, intuitive thing to do would be to cut and paste the array into another file.</p><pre># amount-owing-list.rb</pre><pre>items = [{ customer: &quot;John&quot;, item: &quot;Soup&quot;, cost: 8.50}, {customer: &quot;Sarah&quot;, item: &quot;Pasta&quot;, cost: 12}, {customer: &quot;John&quot;, item: &quot;Coke&quot;, cost: 2.50}]</pre><p>and</p><pre># amount-owing.rb</pre><pre>puts &quot;Enter a customer&#39;s name.&quot;</pre><pre>customer_owing = gets.chomp.capitalize</pre><pre>owed = 0</pre><pre># Cycle through array of hashes</pre><pre>array_elem = items.each do |hash|</pre><p>How do we link these two files together? That’s where the first point of this post comes in.</p><h3>Require_relative</h3><p>From the <a href="http://ruby-doc.org/core-2.6/Kernel.html#method-i-require_relative">docs</a>:</p><blockquote>Ruby tries to load the library named <em>string</em> relative to the requiring file’s path. If the file’s path cannot be determined a <a href="http://ruby-doc.org/core-2.6/LoadError.html">LoadError</a> is raised. If a file is loaded true is returned and false otherwise.</blockquote><p>So by saving our two files in the same folder, require_relative will be able to load amount-owing-list.rb into amount-owing.rb. We simply need to add <strong>require_relative(&quot;amount-owing-list.rb&quot;)</strong> to the latter.</p><p>BUT we still won’t be able to use our array in amount-owing.rb.</p><h3>Global variables</h3><p>From the docs:</p><blockquote>A global variable has a name beginning with $. It can be referred to from anywhere in a program. Before initialization, a global variable has the special value nil.</blockquote><blockquote>Global variables should be used sparingly. They are dangerous because they can be written to from anywhere. Overuse of globals can make isolating bugs difficult; it also tends to indicate that the design of a program has not been carefully thought out.</blockquote><p>We simply need to add $ to the front of items to make it a global variable. This needs to be done in both files, wherever the variable name is mentioned.</p><p>And voila! amount-owing.rb will have access to the global items variable in a separate file.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffefb5f3b886" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bug-bytes/global-variables-across-multiple-files-ruby-ffefb5f3b886">Global variables across multiple files (Ruby)</a> was originally published in <a href="https://medium.com/bug-bytes">Bug Bytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debug of the Day #1 — Ruby types (novice)]]></title>
            <link>https://medium.com/bug-bytes/debug-of-the-day-1-ruby-types-novice-58e46e3df5bc?source=rss----bec57b3080cd---4</link>
            <guid isPermaLink="false">https://medium.com/p/58e46e3df5bc</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[Ewe Lin Loo]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 07:47:36 GMT</pubDate>
            <atom:updated>2019-04-04T07:51:20.986Z</atom:updated>
            <content:encoded><![CDATA[<h3>Gets and input types (Ruby)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yYufkw-st0AOQxh1vSdcnw.png" /></figure><p>This post is all about the gets method. The below is some basic source code that contains a crucial bug. See if you can spot the bug!</p><pre>puts “What is your name?”</pre><pre>name = gets.chomp</pre><pre>puts “Hi, #{name}! How old are you?”</pre><pre>age = gets.chomp</pre><pre>puts “Wow! You are #{age} years old. Congratulations!”</pre><pre>puts “#{name}, in #{100 — age} years, you will be 100 years old!”</pre><p>The issue here is that the method getsstores the user input <em>as a string. </em>This means that it is unable to calculate the expression in the final string interpolation #{100-age).</p><p>To fix this, we simply need to rewrite the string interpolation as #{100-age.to_i}. The .to_i method converts the string to an integer, which allows the expression 100-age to be calculated.</p><p>The best way to do this is to re-write the variable assignment of age as age = gets.chomp.to_i. This would allow age to always be used as an integer if the code were to be expanded.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58e46e3df5bc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bug-bytes/debug-of-the-day-1-ruby-types-novice-58e46e3df5bc">Debug of the Day #1 — Ruby types (novice)</a> was originally published in <a href="https://medium.com/bug-bytes">Bug Bytes</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>