<?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: Gernot Gradwohl</title>
    <description>The latest articles on DEV Community by Gernot Gradwohl (@choallin).</description>
    <link>https://dev.to/choallin</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%2F282704%2F6d9c3c6b-1609-42aa-a8f0-8a954a15f73b.jpeg</url>
      <title>DEV Community: Gernot Gradwohl</title>
      <link>https://dev.to/choallin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/choallin"/>
    <language>en</language>
    <item>
      <title>How to Load Code in Ruby</title>
      <dc:creator>Gernot Gradwohl</dc:creator>
      <pubDate>Thu, 27 Apr 2023 10:07:16 +0000</pubDate>
      <link>https://dev.to/appsignal/how-to-load-code-in-ruby-2bok</link>
      <guid>https://dev.to/appsignal/how-to-load-code-in-ruby-2bok</guid>
      <description>&lt;p&gt;There are many ways to load code and access file-related constants in Ruby. We can create a clear architecture by separating and handling concerns into classes and pulling in only the classes we depend on.&lt;/p&gt;

&lt;p&gt;Many full-stack frameworks like Rails and Hanami offer a built-in method to access the classes we want, as long as we stick with a certain convention. How does this work?&lt;/p&gt;

&lt;p&gt;In this post, we will explore three different options for loading code: using &lt;code&gt;load&lt;/code&gt;, &lt;code&gt;require&lt;/code&gt;, and &lt;code&gt;autoload&lt;/code&gt;. We will also look into the Ruby gem &lt;code&gt;Zeitwerk&lt;/code&gt;. &lt;code&gt;Zeitwerk&lt;/code&gt; is the default code-loading mechanism for many new projects, and a lot of established projects are switching to this great gem. Rails and Hanami both utilize &lt;code&gt;Zeitwerk&lt;/code&gt; as their code-loading tool.&lt;/p&gt;

&lt;p&gt;Before we dive into the code loading options on offer in Ruby, let's take a quick look at &lt;code&gt;$LOAD_PATH&lt;/code&gt; to get to grips with how code loading works.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;$LOAD_PATH&lt;/code&gt; in Ruby
&lt;/h2&gt;

&lt;p&gt;Before we dive deeper into this topic, let's summarize the &lt;code&gt;$LOAD_PATH&lt;/code&gt; in Ruby, as it is essential in understanding how loading works.&lt;/p&gt;

&lt;p&gt;The global variable &lt;code&gt;$LOAD_PATH&lt;/code&gt; or shorter &lt;code&gt;$:&lt;/code&gt; references all directories with Ruby source files registered in an array. So when you prepend &lt;code&gt;bundle exec&lt;/code&gt; with your program name, Ruby adds all the program gems to the load path.&lt;/p&gt;

&lt;p&gt;Then, when we load or require a file, Ruby iterates over this array and searches for the file name. This can add significant time to the booting process, as we might have to search for a given file in many places on the hard disk.&lt;/p&gt;

&lt;p&gt;Keeping that in mind, we'll now explore some Ruby code-loading options in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Options for Loading Code in Ruby
&lt;/h2&gt;

&lt;p&gt;We have three main options for loading code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using the &lt;code&gt;load&lt;/code&gt; method. This loads and parses a Ruby program in a specified file every time you call the method.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;require&lt;/code&gt; method. With this, we load and parse a given file only once.&lt;/li&gt;
&lt;li&gt;The third option is &lt;code&gt;autoload&lt;/code&gt;. We declare upfront that Ruby should require a specified file when we use a constant that doesn't exist yet.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;load&lt;/code&gt; a File in Ruby
&lt;/h2&gt;

&lt;p&gt;As mentioned, with &lt;code&gt;load&lt;/code&gt; we parse and execute a Ruby file.&lt;br&gt;
If the filename we set as an argument is a relative path, i.e., it starts with &lt;code&gt;./&lt;/code&gt; or &lt;code&gt;../&lt;/code&gt;, the file will be loaded relative to the current working directory.&lt;/p&gt;

&lt;p&gt;Remember: This doesn't mean it's relative to the file where the load is located — it's instead relative to the file that started the Ruby process. If you want to know what the working directory is, run &lt;code&gt;Dir.pwd&lt;/code&gt;. Otherwise, Ruby searches for a file with the given name in the &lt;code&gt;$LOAD_PATH&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see how load works:&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;# foo.rb&lt;/span&gt;
&lt;span class="vg"&gt;$x&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;# define global Variable&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm doing it now"&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;Then we load the file with &lt;code&gt;load "foo.rb"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What happens when we load this file? The global variable &lt;code&gt;$x&lt;/code&gt; is created and set to the integer 4. The constant &lt;code&gt;Foo&lt;/code&gt; is also created, and we can instantiate an object from it. So far, this seems just like the more familiar &lt;code&gt;require&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;But what happens when we &lt;code&gt;load&lt;/code&gt; this file again? If we have changed our global value &lt;code&gt;$x&lt;/code&gt;, it resets to 4.&lt;/p&gt;

&lt;p&gt;In older Ruby versions, we also might get a warning that the constant &lt;code&gt;Foo&lt;/code&gt; is being redefined.&lt;/p&gt;

&lt;p&gt;Another problem with &lt;code&gt;load&lt;/code&gt; is that every reload takes some time to run as the Ruby VM is &lt;code&gt;eval&lt;/code&gt;-ing the file. &lt;code&gt;require&lt;/code&gt;, on the other hand, loads and parses a file just once, during the first run.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping the File with &lt;code&gt;load&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Right now, every time we load or require a file, we pollute the global namespace. To avoid this, we can use the &lt;code&gt;load&lt;/code&gt; method with the wrap parameter set to &lt;code&gt;true&lt;/code&gt;. This will create an anonymous module where all the constants and methods defined in a loaded file are placed. Here is an example:&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;# file to be loaded foo.rb&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; Here we will see where our Foo class is located in&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm doing it now"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;###############################&lt;/span&gt;
&lt;span class="c1"&gt;# Now in another file run this:&lt;/span&gt;

&lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="s2"&gt;"foo.rb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run this, Ruby will output something like this: &lt;code&gt;#&amp;lt;Module:0x00007fa3430682e8&amp;gt;::Foo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Why is this useful? For one thing, we are not polluting the global namespace. But since Ruby creates an anonymous module, we can only access the stuff we load with some trickery.&lt;/p&gt;

&lt;p&gt;Nevertheless, wrapping the file is still useful if we want to configure something in our application or set some things up. This way, we can achieve what we want without leaving anything behind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing the Wrapped File Constants
&lt;/h3&gt;

&lt;p&gt;There are two ways to access the constants we create in the wrapped file. The first and most obvious is to set a module name as the argument for the wrap parameter.&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;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm doing it now"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;###############################&lt;/span&gt;
&lt;span class="c1"&gt;# Now in another file run this:&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Parent&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="s2"&gt;"./code_loading/file.rb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Parent&lt;/span&gt;

&lt;span class="no"&gt;Parent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is how we can have access to the anonymous Ruby module:&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;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm doing it now"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;throw&lt;/span&gt; &lt;span class="ss"&gt;:wrapper&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nesting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;last&lt;/span&gt;


&lt;span class="c1"&gt;############################################&lt;/span&gt;
&lt;span class="c1"&gt;# When we load the file we catch the  error&lt;/span&gt;
&lt;span class="c1"&gt;# and make the module accessible&lt;/span&gt;

&lt;span class="n"&gt;mod&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;catch&lt;/span&gt; &lt;span class="ss"&gt;:wrapper&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="s2"&gt;"./code_loading/file.rb"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;mod&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is a hidden problem with &lt;code&gt;wrap&lt;/code&gt;. If the author of the loaded file does something like this, it will break the sandbox:&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="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Foo&lt;/span&gt;
  &lt;span class="c1"&gt;# stuff&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 &lt;code&gt;Foo&lt;/code&gt; is in the global namespace, and there is no way to prevent this pollution.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby Code Loading with &lt;code&gt;require&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Just like the &lt;code&gt;load&lt;/code&gt; method, &lt;code&gt;require&lt;/code&gt; is a method in the &lt;code&gt;Kernel&lt;/code&gt; module. The big difference between these two methods is how they behave on multiple calls.&lt;/p&gt;

&lt;p&gt;While &lt;code&gt;load&lt;/code&gt; always reevaluates a file, &lt;code&gt;require&lt;/code&gt; doesn't. Its response is also different. If you call &lt;code&gt;require&lt;/code&gt; for the first time and the method actually loads something, it returns &lt;code&gt;true&lt;/code&gt;, otherwise, it returns &lt;code&gt;false&lt;/code&gt;. &lt;code&gt;load&lt;/code&gt;, on the other hand, always returns &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Some pseudo-code for &lt;code&gt;require&lt;/code&gt; would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;already_loaded_files?&lt;/span&gt; &lt;span class="n"&gt;file_path&lt;/span&gt;
    &lt;span class="nb"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;find_file_in_load_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file_path&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
    &lt;span class="n"&gt;mark_file_as_loaded!&lt;/span&gt; &lt;span class="n"&gt;file_path&lt;/span&gt;
    &lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="kp"&gt;false&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;&lt;code&gt;require&lt;/code&gt; accepts absolute paths for files as well as just a simple name. If we need a simple file, &lt;code&gt;require&lt;/code&gt; also searches in the &lt;code&gt;$LOAD_PATH&lt;/code&gt;. This, of course, means that we still have the same disadvantages we had with &lt;code&gt;load&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Simpler File Searching with &lt;code&gt;require_relative&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;require_relative&lt;/code&gt; always looks for a file that is relative to the current file you are working on. &lt;code&gt;require_relative&lt;/code&gt; doesn't iterate over a directory, and no real search is involved. Ruby looks into the path specified and tries to load the file. If it isn't there, we get a &lt;code&gt;LoadError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately, as of now, &lt;code&gt;require&lt;/code&gt; doesn't have a &lt;code&gt;wrap&lt;/code&gt; parameter like &lt;code&gt;load&lt;/code&gt; does, so &lt;code&gt;require&lt;/code&gt; always pollutes the global namespace. But there has been &lt;a href="https://bugs.ruby-lang.org/issues/10320"&gt;some recent discussion on the Ruby issue tracker&lt;/a&gt; to add something like &lt;code&gt;wrap&lt;/code&gt; to the &lt;code&gt;require&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;autoload&lt;/code&gt; Code Loading in Ruby
&lt;/h2&gt;

&lt;p&gt;Our third option for code loading in Ruby is &lt;code&gt;autoload&lt;/code&gt;. With &lt;code&gt;autoload&lt;/code&gt;, we tell the Ruby VM upfront what constants might be accessed and where to find and load them.&lt;/p&gt;

&lt;p&gt;A big advantage of this approach is that we only load the files we really need. Therefore, the booting process can be really fast as we just pay for what we use. When we actually need the constant, &lt;code&gt;autoload&lt;/code&gt; requires the file with the method &lt;code&gt;require&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's how we set up an autoload:&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;### lib/services/foo.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Services&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"bar"&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;end&lt;/span&gt;

&lt;span class="c1"&gt;### lib/services.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Services&lt;/span&gt;
  &lt;span class="nb"&gt;autoload&lt;/span&gt; &lt;span class="ss"&gt;:Foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/services/foo.rb"&lt;/span&gt;

  &lt;span class="c1"&gt;# Now we have access to the Foo constant in this module&lt;/span&gt;
  &lt;span class="c1"&gt;# and we will load the file when we actually need it.&lt;/span&gt;

  &lt;span class="c1"&gt;# Foo.new.bar =&amp;gt; loads the file and prints "bar"&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 most popular gem for managing loading files is &lt;code&gt;Zeitwerk&lt;/code&gt;, and it works with &lt;code&gt;autoload&lt;/code&gt;. Now let's see how Zeitwerk works and how to set it up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Code Loading with Zeitwerk
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/fxn/zeitwerk"&gt;Zeitwerk&lt;/a&gt; takes a directory and makes every file underneath it available to load. The convention is that every new sub-directory is a new module, and every file defines a class with the same name as the file.&lt;/p&gt;

&lt;p&gt;Zeitwerk offers an option for code reloading that is useful during development. Rails uses this mode in an active development environment.&lt;/p&gt;

&lt;p&gt;How does it work under the hood? Here is some &lt;em&gt;very, very&lt;/em&gt; simplified pseudo-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="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Zeitwerk&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Loader&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;@root_dir&lt;/span&gt; &lt;span class="o"&gt;=&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;def&lt;/span&gt; &lt;span class="nf"&gt;push_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="vi"&gt;@root_dirs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;dir&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;setup&lt;/span&gt;
      &lt;span class="c1"&gt;# scan root dirs and define autoloads for each file&lt;/span&gt;
      &lt;span class="vi"&gt;@root_dirs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
        &lt;span class="no"&gt;Dir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
          &lt;span class="k"&gt;next&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;file&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="n"&gt;file&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;".."&lt;/span&gt;

          &lt;span class="nb"&gt;autoload&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class_name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_sym&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dir&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;span class="k"&gt;end&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;loader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Zeitwerk&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, with the last call to &lt;code&gt;loader.setup&lt;/code&gt;, we get access to every constant defined in one of the files relative to the current file.&lt;br&gt;
Of course, this simplified code doesn't highlight many of the other great features that &lt;code&gt;Zeitwerk&lt;/code&gt; has, like eager loading and handling namespaces. That's outside of the scope of this post, and I'll leave it to you to dig deeper and explore all &lt;code&gt;Zeitwerk&lt;/code&gt; has to offer.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to Set Up &lt;code&gt;Zeitwerk&lt;/code&gt; with Roda
&lt;/h3&gt;

&lt;p&gt;Since Rails and Hanami use &lt;code&gt;Zeitwerk&lt;/code&gt; as their default, let's see how we can configure &lt;code&gt;Zeitwerk&lt;/code&gt; for Roda:&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;### in config.ru&lt;/span&gt;

&lt;span class="n"&gt;loader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Zeitwerk&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push_dir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;__dir__&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/lib"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'RACK_ENV'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'production'&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;
  &lt;span class="no"&gt;Zeitwerk&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eager_load_all&lt;/span&gt;
  &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;App&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enable_reloading&lt;/span&gt;
  &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setup&lt;/span&gt;
  &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reload&lt;/span&gt;
    &lt;span class="no"&gt;App&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we configure our app like this, we have code reloading during development and eager loading in production. This way, the boot time during development is fast, as only the files that are immediately needed are loaded. In production, we load everything upfront and have a faster response time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;In this post, we first took a quick look at &lt;code&gt;$LOAD_PATH&lt;/code&gt; in Ruby to lay the foundations of how code loading works. We then explored three options to load code in some detail: &lt;code&gt;load&lt;/code&gt;, &lt;code&gt;require&lt;/code&gt;, and &lt;code&gt;autoload&lt;/code&gt; (which works well with the &lt;code&gt;Zeitwerk&lt;/code&gt; gem).&lt;/p&gt;

&lt;p&gt;So, how should we load our code? Most of the time, we should use &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;Zeitwerk&lt;/code&gt; to automate that. If you have special requirements and want to put loaded code into a namespace, the &lt;code&gt;load&lt;/code&gt; method with a wrap parameter is a good option.&lt;/p&gt;

&lt;p&gt;Happy code loading!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P.S. If you'd like to read Ruby Magic posts as soon as they get off the press, &lt;a href="https://blog.appsignal.com/ruby-magic"&gt;subscribe to our Ruby Magic newsletter and never miss a single post&lt;/a&gt;!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
    <item>
      <title>Under Observation — Variable Scoping and How It Affects Your Program</title>
      <dc:creator>Gernot Gradwohl</dc:creator>
      <pubDate>Mon, 20 Apr 2020 12:29:26 +0000</pubDate>
      <link>https://dev.to/choallin/under-observation-variable-scoping-and-how-it-affects-your-program-951</link>
      <guid>https://dev.to/choallin/under-observation-variable-scoping-and-how-it-affects-your-program-951</guid>
      <description>&lt;p&gt;Let’s take a look at taking a look&lt;/p&gt;

&lt;p&gt;When someone begins in programming, one of the basic concepts they are introduced to is name resolution — aka, scoping. Scopes are very important because they determine if a variable/method is visible or not. Also, scopes help create a tidy and clean global namespace, because you can “hide” your variables in a subscope.&lt;/p&gt;

&lt;p&gt;After learning the basics of programming, scopes still remain very important because they are essential for knowing what is going on in a more advanced concept, metaprogramming. Especially in Ruby, many metaprogramming facilities work by bending the scope code is executed under. For example, the methods class_eval or instance_eval will bend the scope to another object/class.&lt;/p&gt;

&lt;p&gt;To grasp that concept better let’s take a closer look at scopes. What type of scopes exist, and what level of scopes are commonly used?&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Scope?
&lt;/h2&gt;

&lt;p&gt;The definition is: A scope is a part of a program.&lt;/p&gt;

&lt;p&gt;This sounds very vague. So let’s make it more clear. A scope is the part of the program where a variable is visible and accessible. The part where a variable is visible can be nested in various levels — more on this later.&lt;/p&gt;

&lt;p&gt;In computer science, there are two types of scoping: lexical and dynamic. The type depends on the programming language you’re using.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lexical Scoping
&lt;/h3&gt;

&lt;p&gt;This is the type that all modern languages implement, as far as I know. Lexical scoping means that the context of the source code is the scope. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_method&lt;/span&gt;
  &lt;span class="n"&gt;method_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="n"&gt;different_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;different_variable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;method_variable&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;All your variables are defined in the context of your current method. Inside the block, we access the variables from the method which is one level above. But when you read the code it’s very clear what the value of method_variable is because of the context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dynamic Scoping
&lt;/h3&gt;

&lt;p&gt;This implementation isn’t used in many languages anymore. Some that are still in use and implement scoping this way are Perl (only if the user opts-in), Bash, Emacs Lisp, and LaTeX.&lt;/p&gt;

&lt;p&gt;So, how does dynamic scoping work? The name resolution depends on the runtime. Let’s adjust the example above so that we can see the difference. (Remember, Ruby doesn’t use dynamic scoping, so this is a pseudo-Ruby code and won’t work in your console).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;multiply_by_two&lt;/span&gt;
  &lt;span class="n"&gt;multiplicand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="n"&gt;test_method&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;multiply_by_three&lt;/span&gt;
  &lt;span class="n"&gt;multiplicand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
  &lt;span class="n"&gt;test_method&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;test_method&lt;/span&gt;
  &lt;span class="n"&gt;different_variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="n"&gt;different_variable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;multiplicand&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;multiply_by_two&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; [2,4,6]&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;multiply_by_three&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [3,6,9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a dynamically-scoped language the runtime looks in the current block for the variable, then it looks in the callstack above to search for the variable. In our example, the method multiply_by_two has the variable multiplicand with 2 initialized and the method multiply_by_three with 3. The result of the test_method really depends on the method that it’s calling.&lt;/p&gt;

&lt;p&gt;With this example, the disadvantage is very clear. Dynamic scoping voids the benefits of referential transparency since it depends on the call stack/runtime and it is very hard to reason about the value of a variable.&lt;/p&gt;

&lt;p&gt;What levels of scoping do we generally have?&lt;/p&gt;

&lt;h2&gt;
  
  
  Levels of Scope
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Global
&lt;/h3&gt;

&lt;p&gt;As the name suggests, global scoping is when a variable or method is visible in the whole application. This is not necessarily bad for small scripts but as your application grows an extensive use of global variables will make your code hard to maintain.&lt;/p&gt;

&lt;p&gt;Also, global variables are especially complicated to get right when it comes to multithreading.&lt;/p&gt;

&lt;h3&gt;
  
  
  Module
&lt;/h3&gt;

&lt;p&gt;In module scoping, the variable is visible inside a module but modules can be structured across multiple files.&lt;/p&gt;

&lt;h3&gt;
  
  
  File
&lt;/h3&gt;

&lt;p&gt;In file scoping the variable is only visible inside a file. This level is mainly used in C (sometimes in C++ as well). In C it is possible to define a variable in a file but not export it, so other functions can’t use it. If the developer wants to use a file-scoped variable they have to define it at the beginning of the file and this must not be inside a function.&lt;/p&gt;

&lt;p&gt;File scoping is very similar to module scoping in the special case that a file is a module.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function
&lt;/h3&gt;

&lt;p&gt;In function scoping, function is defined in a variable and accessible in any sub-block of that function.&lt;/p&gt;

&lt;p&gt;This is the way we used the variable method_variable in our example for lexical scoping.&lt;/p&gt;

&lt;p&gt;In JavaScript functions, function scoping is the default scope level for every variable that is initialized with the var keyword (if a variable is initialized without any keyword, it becomes a global variable by default). Why is that the case? Because JavaScript puts any of those variables implicit at the top of the function.&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;function&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Interesting"&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the variable x is still available although it is defined in a sub-block of the function.&lt;/p&gt;

&lt;p&gt;The implicit change of order for the variables with var is used to make some funny code work:&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;function&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Interesting…"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Up-to-date Chrome or Firefox doesn’t work like this anymore, but still, a variable defined with var is visible in the whole function not just in a block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Block
&lt;/h3&gt;

&lt;p&gt;A function can be divided into sub-blocks. In many C-like languages a block is surrounded by {} — in the case of Ruby, we use do end more often.&lt;/p&gt;

&lt;h3&gt;
  
  
  Expression
&lt;/h3&gt;

&lt;p&gt;In many functional languages, we have an additional level, the expression level. This means that a variable is only defined in a short expression.&lt;/p&gt;

&lt;p&gt;This can look like this:&lt;/p&gt;

&lt;p&gt;let val x = some_function() in x * x end&lt;/p&gt;

&lt;p&gt;Here we are using the variable x as a cache so we don’t have to call the function some_function() twice.&lt;/p&gt;

&lt;p&gt;Note: The let keyword exists in non-functional languages as well — e.g. in JavaScript or Rust—but in these languages, it has nothing to do with how the variable is scoped.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connected Concepts
&lt;/h2&gt;

&lt;p&gt;Dynamic dispatch is very closely connected to scoping. This describes how to resolve instance variables/methods for objects (most of the time).&lt;/p&gt;

&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;Sometimes, closures are confusing when it comes to scoping. Why is that so? Because generally speaking they are executed in a different place than where they are coded.&lt;/p&gt;

&lt;p&gt;Nevertheless, they are lexically scoped because the context of the code is relevant for name resolution rather than the runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If we carefully consider the scoping of our code and remain aware of the context it is executed in, we’ll be able to write better code and not become confused when we are using domain-specific languages (which bend scopes) — something quite common in the Ruby community, especially in Rails, but not too uncommon in JavaScript as well.&lt;/p&gt;

</description>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
