<?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[Launch School Study Guide - Medium]]></title>
        <description><![CDATA[Key concepts from the Launch School Core Curriculum - Medium]]></description>
        <link>https://medium.com/launch-school-study-guide-unofficial?source=rss----d702ad906078---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Launch School Study Guide - Medium</title>
            <link>https://medium.com/launch-school-study-guide-unofficial?source=rss----d702ad906078---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 07:18:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/launch-school-study-guide-unofficial" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Course RB109 Part 1 — Ruby local variable scope]]></title>
            <link>https://medium.com/launch-school-study-guide-unofficial/course-rb109-part-1-ruby-local-variable-scope-8c3f1b2cb66c?source=rss----d702ad906078---4</link>
            <guid isPermaLink="false">https://medium.com/p/8c3f1b2cb66c</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[launch-school]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Kelvin Wong]]></dc:creator>
            <pubDate>Tue, 15 Jan 2019 08:11:04 GMT</pubDate>
            <atom:updated>2019-01-15T08:13:31.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>Course RB109 Part 1 — Ruby local variable scope</h3><p><em>A summary on local variable scope in Ruby, for beginners</em></p><h3>Quick background</h3><p>I’m currently enrolled in <a href="https://launchschool.com/">Launch School</a>, an online, fully remote Software Engineering School, billed as ‘The Slow Path for Studious Beginners to a Career in Software Development’.</p><p>This post is part of my preparation for Launch School’s Course RB109 on Ruby and General Programming. I’ve attempted to summarise what I’ve learnt so far on local variable scope in Ruby, mainly for my own benefit, but hopefully for yours too!</p><h3>Contents</h3><ol><li>What is a ‘local variable’?</li><li>The trivial case — local variables defined outside of any block or method</li><li>Local variables defined within a block</li><li>Local variables defined within a method definition</li><li>Local variables defined within method invocation</li><li>Summary</li></ol><h3><strong>1. What is a ‘local variable’?</strong></h3><p>A <strong>variable</strong> is a ‘container for a Ruby object’; it stores the reference to a given Ruby object (i.e. its object ID), so that this object can be used or changed later in a program.</p><p><strong>Local</strong> in <strong>local variable</strong> means that such a variable may not be available to use everywhere in a larger overall program; its value can only be accessed in specific, smaller parts of the program, or within its <strong>scope</strong>.</p><p>This post’s purpose is precisely to explain the different possibilities for the <strong>scope</strong> of a <strong>local variable</strong>.</p><p>If we’re to summarise this entire post in one sentence, it’d be:</p><blockquote>A local variable’s scope is the block, method definition or program (whichever is ‘smaller’) in which it was first defined.</blockquote><p>We now examine how this rule plays out in actual code.</p><h3><strong>2. The trivial case — local variables defined outside of any block or method</strong></h3><pre>a = 3<br>b = a + 5<br>#=&gt; 8</pre><p>In this first example above, we first declare the local variable a on line 1, assigning it to 3. On line 2, a + 5 evaluates 8, to which we then assign b.</p><p>It’s clear that a is available throughout the whole program; in other words, its <strong>scope</strong> is the entire program.</p><p>In the language of our one-sentence ‘rule’:</p><blockquote>A local variable’s scope is the block, method definition or program (whichever is ‘smaller’) in which it was first defined.</blockquote><p>Since there are no blocks or methods in this program, a’s <strong>scope</strong> is simply the entire program in which was defined.</p><h3>3. Local variables defined within a block</h3><pre>loop do<br>  a = 3<br>  break<br>end</pre><pre>b = a + 5<br># =&gt; NameError: undefined local variable or method `a&#39; for main:Object</pre><p>This time, the variable a was defined inside the block defined by do on line 1, and end on line 4. When we try to access it on line 6 b = a + 5, Ruby throws a NameError.</p><p>This is because if a was first defined within a block, it is only available for use inside that block. After the block has been executed, the variable a is no longer available.</p><p>Going back to our one-sentence ‘rule’:</p><blockquote>A local variable’s scope is the block, method definition or program (whichever is ‘smaller’) in which it was first defined.</blockquote><p>Since we have no methods in this program, a&#39;s <strong>scope</strong> is either the block in which it was first defined, or the entire program, whichever is ‘smaller’ — which in this case is clearly the block it was first defined.</p><h4>3a) Expanding a local variable’s scope beyond a block</h4><p>If we want a variable inside a block to be accessible again after block execution ends, we must first initialize it in the program, outside of the block:</p><pre>a = 0</pre><pre>loop do<br>  a = 3<br>  break<br>end</pre><pre>b = a + 5<br># =&gt; 8</pre><p>Now, a is first defined in the main program, so correspondingly, its <strong>scope</strong> is the entire program. This includes inside the block in lines 3–6, AND outside the block from line 7 onwards.</p><p>There is of course another way to define blocks, using the curly brackets{}. So here is an equivalent program, with the same <strong>scope</strong> for a:</p><pre>a = 0</pre><pre>loop {<br>  a = 3<br>  break<br>}</pre><pre>b = a + 5<br># =&gt; 8</pre><p>This illustrates an important rule about local variable <strong>scope</strong> in Ruby, a direct corollary of our one-sentence ‘rule’:</p><blockquote>Local variables first defined in an outer scope can be accessed in an inner scope, but not vice versa.</blockquote><ul><li>When a was initialized outside of the block (in the ‘outer <strong>scope</strong>’), it could be accessed inside the block (the ‘inner <strong>scope</strong>’).</li><li>But when it was first defined inside the block (the ‘inner <strong>scope</strong>’), it was no longer accessible in the main program (the ‘outer <strong>scope</strong>’) once block execution ended.</li></ul><h4><strong>3b) ‘Exception’ 1: local variables defined within a ‘</strong>for’<strong> loop</strong></h4><pre>for i in 1..3 do<br>  a = i<br>end</pre><pre>a # =&gt; ?</pre><p>At first glance, this program would appear to return a NameError as before, since a wasn’t initialized before it appears between do on line 1 and end on line 3.</p><p>However, here it actually returns 3.</p><p>The reason is because the body of a for loop is actually is part of the main program, and do and end in for loops do not define a block. So the above program is equivalent to:</p><pre>i = 1<br>a = i</pre><pre>i = 2<br>a = i</pre><pre>i = 3<br>a = i</pre><pre>a # =&gt; 3</pre><p>It follows that a&#39;s <strong>scope</strong> extends throughout the entire program.</p><h4><strong>3c) ‘Exception’ 2: local variables initialized within an ‘</strong>if’<strong> statement</strong></h4><p>The situation is similar with if statements, which are part of the main Ruby program.</p><pre>if 4 &gt; 3<br>  a = 3<br>end</pre><pre>a # =&gt; 3</pre><p>Here, 4 &gt; 3 in the if statement evaluates to true, so the program simply starts executing the next line, line 2, of the main program. Despite the presence of end on line 3, a = 3 on line 2 is not part of any block.</p><p>Therefore, since a here is first defined in the main program, its <strong>scope</strong> is the entire program. It is available for use inside the if statement, as well as afterwards.</p><h3>4. Local variables defined within a method definition</h3><pre>def some_method<br>  a = 3<br>  a + 5<br>end</pre><pre>a<br># =&gt; NameError: undefined local variable or method `a&#39; for main:Object</pre><p>In this case, we now have a variable a that is first defined within the definition of a method.</p><p>We go back to our rule:</p><blockquote>A local variable’s scope is the block, method definition or program (whichever is ‘smaller’) in which it was first defined.</blockquote><p>So a&#39;s <strong>scope</strong> is just within the method definition in which it was first defined. Once method definition ends on line 4, a&#39;s <strong>scope</strong> ends and it’s no longer available for use, hence the NameError when we try to return its value on line 6.</p><h4>4a) But what if we try to initialize it before the method definition?</h4><p>What about this?</p><pre>a = 0</pre><pre>def some_method<br>  a = 3<br>  a + 5<br>end</pre><pre>a # =&gt; ?</pre><p>We first initialize a on line 1. Does a get re-assigned to 3 by the method definition, or does it stay as 0?</p><p>The answer: it does not get re-assigned.</p><pre>a = 0</pre><pre>def some_method<br>  a = 3<br>  a + 5<br>end</pre><pre>a # =&gt; 0</pre><p>This is because the a in the method definition is actually a different local variable to the a outside the method definition. The above program is equivalent to:</p><pre>a_outer = 0</pre><pre>def some_method<br>  a_inner = 3<br>  a_inner + 5<br>end</pre><pre>a_outer # =&gt; 0</pre><p>This behaviour may seem different to local variables defined in blocks, but it actually makes intuitive sense. A method should be a standalone piece of functionality that’s independent of the main program, and we should need to care only about its inputs and outputs. That its definition happens to include a variable with the same name as one outside in the main program, shouldn’t matter — and it doesn’t.</p><p>Even though using variable names in this way doesn’t affect the program’s functionality, for clarity it’s best to avoid doing this if possible.</p><h3>5. Local variables defined within method invocation</h3><p>This case is in fact similar to our case #3 from earlier. An example:</p><pre>[1, 2, 3].each do |num|<br>  a = 3<br>  puts num + a<br>end</pre><pre>a # =&gt; NameError: undefined local variable or method `a&#39; for main:Object</pre><p>a here is first defined inside a block that’s passed to the each method when #each is called on line 1, and so its <strong>scope </strong>ends after the block.</p><p>And similarly, we can initialize a in the main program before the block, if we want it to be available afterwards:</p><pre>a = 0</pre><pre>[1, 2, 3].each do |num|<br>  a = 3<br>  puts num + a<br>end</pre><pre>a # =&gt; 3</pre><h4>5a) Variable shadowing in method invocations</h4><pre>a = 0</pre><pre>[1, 2, 3].each do |a|<br>  puts a<br>end</pre><pre>a # =&gt; ?</pre><p>We’ve initialized a before the block. a is assigned to 3 in the array on the last line in the execution of each. Is a still 3 after the method invocation?</p><p>The answer is no:</p><pre>a = 0</pre><pre>[1, 2, 3].each do |a|<br>  puts a<br>end</pre><pre>a # =&gt; 0</pre><p>This is because the parameter a in |a| in the block on line 3 is a newly defined local variable whose <strong>scope </strong>is only inside of the block.</p><p>The a on line 4 refers to this variable, rather than the a that was first declared on line 1.</p><p>After block execution ends, the a defined inside the block is no longer available, and the a on line 7 refers back to the a we defined on line 1.</p><p>The above program is equivalent to:</p><pre>a_outer = 0</pre><pre>[1, 2, 3].each do |a_inner|<br>  puts a_inner<br>end</pre><pre>a_outer # =&gt; 0</pre><p>When this happens — when we happen to define a block parameter that has the same name as a variable outside of the block — it’s called variable shadowing.</p><p>While it doesn’t affect the functionality of the program, it’s something we should avoid, for clarity.</p><h3>6) Summary</h3><p>That’s it for now! This is by no means a complete summary, but hopefully what it did cover has been useful.</p><p>Remember, when in doubt, go back to our one-sentence ‘rule’:</p><blockquote>A local variable’s scope is the block, method definition or program (whichever is ‘smaller’) in which it was first defined.</blockquote><p>So for any local variable, look for the smallest block, method definition or program where it was first defined — that’s going to be its <strong>scope</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c3f1b2cb66c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/launch-school-study-guide-unofficial/course-rb109-part-1-ruby-local-variable-scope-8c3f1b2cb66c">Course RB109 Part 1 — Ruby local variable scope</a> was originally published in <a href="https://medium.com/launch-school-study-guide-unofficial">Launch School Study Guide</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>