<?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: Josh Smith</title>
    <description>The latest articles on DEV Community by Josh Smith (@joshdevhub).</description>
    <link>https://dev.to/joshdevhub</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%2F749149%2F6633bc29-ea0d-4994-bdc7-b2315fae8917.jpeg</url>
      <title>DEV Community: Josh Smith</title>
      <link>https://dev.to/joshdevhub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshdevhub"/>
    <language>en</language>
    <item>
      <title>Configuring IRB autocomplete</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Tue, 20 Feb 2024 20:36:36 +0000</pubDate>
      <link>https://dev.to/joshdevhub/configuring-irb-autocomplete-3n4d</link>
      <guid>https://dev.to/joshdevhub/configuring-irb-autocomplete-3n4d</guid>
      <description>&lt;p&gt;Since the release of Rails 7, the Rails console comes with a super cool autocomplete feature that provides suggestions to you as you type. This is much like the intellisense and snippets you get while writing code in an editor. But let's take a look at what this autocomplete looks like out of the box:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febvtdtf0gxvzfrr8giuj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febvtdtf0gxvzfrr8giuj.png" alt="IRB autocompletion"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yuck.&lt;/p&gt;

&lt;p&gt;This might look okay for you depending on the color setup of your terminal, but for me the contrast is almost poor enough to render it unusable. Fortunately this can be changed! Let's explore how.&lt;/p&gt;

&lt;h3&gt;
  
  
  Time for a Facelift
&lt;/h3&gt;

&lt;p&gt;Until recently there was no good way to configure IRB's autocomplete styling. But a few months ago, a &lt;a href="https://github.com/ruby/reline/pull/552" rel="noopener noreferrer"&gt;&lt;code&gt;Reline::Face&lt;/code&gt;&lt;/a&gt; class was added to IRB that offers a simple public API for configuring the colors of the autocomplete dialog.&lt;/p&gt;

&lt;p&gt;Using this, I can get it looking &lt;em&gt;much&lt;/em&gt; nicer:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foe210sre8gk7awcyb31g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foe210sre8gk7awcyb31g.png" alt="IRB better autocomplete"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To start with, you'll want to create a new file in your home directory called &lt;code&gt;.irbrc&lt;/code&gt;. The contents of this file are executed before an IRB session starts, so you can use it to configure certain properties of your IRB sessions.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="c"&gt;# to create the file&lt;/span&gt;
&lt;span class="nb"&gt;touch&lt;/span&gt; ~/.irbrc

&lt;span class="c"&gt;# to edit the file&lt;/span&gt;
code ~/.irbrc &lt;span class="c"&gt;# or `vim` or `subl` or whatever you use&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In this file, I have the following code set to configure the autocompletion dialog:&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;# configure autocomplete dialog colors&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;defined?&lt;/span&gt; &lt;span class="no"&gt;Reline&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Face&lt;/span&gt;
  &lt;span class="no"&gt;Reline&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Face&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:completion_dialog&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;conf&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:default&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;foreground: &lt;/span&gt;&lt;span class="s2"&gt;"#cad3f5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;background: &lt;/span&gt;&lt;span class="s2"&gt;"#363a4f"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:enhanced&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;foreground: &lt;/span&gt;&lt;span class="s2"&gt;"#cad3f5"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;background: &lt;/span&gt;&lt;span class="s2"&gt;"#5b6078"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;define&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:scrollbar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;foreground: &lt;/span&gt;&lt;span class="s2"&gt;"#c6a0f6"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;background: &lt;/span&gt;&lt;span class="s2"&gt;"#181926"&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;else&lt;/span&gt;
  &lt;span class="no"&gt;IRB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:USE_AUTOCOMPLETE&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To walk through this some, I first check &lt;code&gt;defined? Reline::Face&lt;/code&gt; because I sometimes work on older projects that don't have these newer features available. When I'm unable to customize through &lt;code&gt;Reline::Face&lt;/code&gt;, I prefer to just disable autocomplete entirely with &lt;code&gt;IRB.conf[:USE_AUTOCOMPLETE] = false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;Reline::Face&lt;/code&gt; does exist, I can define the colors used by &lt;code&gt;:default&lt;/code&gt;, &lt;code&gt;:enhanced&lt;/code&gt;, and &lt;code&gt;:scrollbar&lt;/code&gt;. The &lt;code&gt;enhanced&lt;/code&gt; option defines what the currently highlighted item in the autocomplete dialog looks like. The &lt;code&gt;default&lt;/code&gt; option defines what every other item in the list looks like. And the &lt;code&gt;scrollbar&lt;/code&gt; of course defines what the scrollbar looks like.&lt;/p&gt;

&lt;p&gt;If your terminal doesn't support truecolor (a common example is the default Terminal app for MacOS), then you won't be able to use full RGB for these. See &lt;a href="https://github.com/ruby/reline/blob/master/doc/reline/face.md" rel="noopener noreferrer"&gt;Reline's docs&lt;/a&gt; for other options.&lt;/p&gt;

&lt;h3&gt;
  
  
  More with IRB
&lt;/h3&gt;

&lt;p&gt;There are several other cool things you can do to configure your experience with IRB. You can set aliases for commonly used methods, you can define methods for more complicated tasks that you find yourself frequently executing, you can &lt;a href="https://twitter.com/_swanson/status/1346851840944730112" rel="noopener noreferrer"&gt;write a custom prompt that tells you if you're in the production or development console&lt;/a&gt;, etc.&lt;/p&gt;

&lt;p&gt;If you're interested in any of this, I definitely recommend reading the &lt;a href="https://docs.ruby-lang.org/en/3.2/IRB.html" rel="noopener noreferrer"&gt;IRB docs&lt;/a&gt;. I like using some of the features around &lt;code&gt;EVAL_HISTORY&lt;/code&gt;, which are disabled by default. Let me know in the comments if you'd like any more IRB-centered posts 😄&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Connecting Debugger to Rails Applications</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Tue, 19 Dec 2023 18:52:08 +0000</pubDate>
      <link>https://dev.to/joshdevhub/connecting-debugger-to-rails-applications-2p4p</link>
      <guid>https://dev.to/joshdevhub/connecting-debugger-to-rails-applications-2p4p</guid>
      <description>&lt;p&gt;In a previous post about &lt;a href="https://dev.to/joshdevhub/debugging-silent-create-action-failures-in-rails-1boo"&gt;debugging silent create action failures&lt;/a&gt;, I touched upon the practice of connecting the Rails web process to a debugger when facing challenging issues. A debugger allows you to methodically step through your code and examine the runtime values for every stage of a process.&lt;/p&gt;

&lt;p&gt;Setting up the debugger can be a bit of a hurdle though, especially if your app is being driven by a &lt;code&gt;Procfile&lt;/code&gt;. I wanted to make a dedicated post about the different ways you can get the debugger up and running in a Rails app. So let's get to it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;br&gt;
This post assumes that you're on a Rails 7 app, which comes out of the box with the ruby debug gem. With earlier versions of Rails, the ruby debug gem wasn't included by default, and it was common to use the &lt;code&gt;pry-byebug&lt;/code&gt; gem for debugging. So if that's the situation you're in, not everything I say in this post will work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Without a Procfile
&lt;/h2&gt;

&lt;p&gt;In the root of your Rails application, run &lt;code&gt;ls Procfile*&lt;/code&gt; in your terminal. If nothing shows up, your application's processes aren't managed by a &lt;code&gt;Procfile&lt;/code&gt;, and you likely just run &lt;code&gt;rails server&lt;/code&gt; to start your app.&lt;/p&gt;

&lt;p&gt;If this is the case, then connecting a debugger to your server is fairly straightforward. Let's take a &lt;code&gt;#create&lt;/code&gt; action on a &lt;code&gt;Post&lt;/code&gt; resource as an example for illustration:&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;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="c1"&gt;# assuming `current_user` is an instance of the User model&lt;/span&gt;
    &lt;span class="vi"&gt;@post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="n"&gt;root_path&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :unprocessable_entity&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's say this post won't save and you want step through this code to analyze why. You can add a breakpoint to the beginning of the &lt;code&gt;#create&lt;/code&gt; action:&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;create&lt;/span&gt;
  &lt;span class="c1"&gt;# aliased as `binding.b` and `debugger`, you can use those too&lt;/span&gt;
  &lt;span class="nb"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;break&lt;/span&gt;

  &lt;span class="c1"&gt;# the actual code ...&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 in your browser with &lt;code&gt;localhost&lt;/code&gt; open, when you try to submit a form to create a new post, you'll see that the browser just hangs as if caught in an infinite loop. This is good thing! Your application is effectively paused at the breakpoint you set.&lt;/p&gt;

&lt;p&gt;If you look in the terminal window that's running the server process, you'll see something like the image below:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxlzfnrw5h5al0zrvtoa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuxlzfnrw5h5al0zrvtoa.png" alt="terminal window with ruby debug prompt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Execution is paused at the breakpoint (which has a little arrow pointing at it). You can then enter commands to the &lt;code&gt;rdbg&lt;/code&gt; prompt to control the debugger. For a list of the different commands you can use, visit the &lt;a href="https://github.com/ruby/debug" rel="noopener noreferrer"&gt;documentation for the debug gem&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  With a Procfile
&lt;/h2&gt;

&lt;p&gt;If you ran &lt;code&gt;ls Procfile*&lt;/code&gt; and one or more &lt;code&gt;Procfile&lt;/code&gt;s were listed, then your application is probably run through one. To start your app, you likely use a tool like &lt;code&gt;foreman&lt;/code&gt;, &lt;code&gt;heroku local&lt;/code&gt;, or some &lt;code&gt;bin/dev&lt;/code&gt; script.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Procfile&lt;/code&gt; tends to be used when there are multiple processes that need to be running for your app to function. This can include things like building CSS/JS, background worker, search engine, etc. A &lt;code&gt;Procfile&lt;/code&gt; will specify the commands for each process that your app requires. Instead of opening new terminal windows for each process, tools like &lt;code&gt;foreman&lt;/code&gt; and &lt;code&gt;heroku local&lt;/code&gt; can streamline your app's setup by using the &lt;code&gt;Procfile&lt;/code&gt; to run these processes from a single terminal instance. This is great for keeping your workspace clean and ensuring you have all the necessary processes running.&lt;/p&gt;

&lt;p&gt;But this introduces some problems if you try to connect a debugger using the flow above. Aforementioned tools like &lt;code&gt;foreman&lt;/code&gt; and &lt;code&gt;heroku local&lt;/code&gt; won't know that they should be listening to and forwarding stdin (your inputs on the terminal) to your web process. So although you can set breakpoints and pause execution in your app, you'll be unable to actually control the debugger.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solutions
&lt;/h3&gt;

&lt;p&gt;There are a few ways connect a debugger when your app is managed by a &lt;code&gt;Procfile&lt;/code&gt;:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Split out the server process
&lt;/h4&gt;

&lt;p&gt;You can isolate the Rails server process from the rest of the commands in the &lt;code&gt;Procfile&lt;/code&gt;. Navigate to your &lt;code&gt;Procfile&lt;/code&gt; (or potentially &lt;code&gt;Procfile.dev&lt;/code&gt; if you have a different one set to run locally), and remove the line that runs the web process. Now when you run the &lt;code&gt;Procfile&lt;/code&gt;, all your processes will start up except the server. Then you can open a new terminal and run the server in isolation with &lt;code&gt;bin/rails server&lt;/code&gt;. With the web process running separately, you'll be able to properly operate the debugger in the terminal that's running the server.&lt;/p&gt;

&lt;p&gt;This is probably the most straightforward method, but it can be a bit inconvenient if you regularly have to do this. If it's your own project or one you control, you can remove the server process from your local &lt;code&gt;Procfile.dev&lt;/code&gt; and always run the server separately. If you're working with others, the team may not be okay with committing this kind of change to the &lt;code&gt;Procfile&lt;/code&gt;. So you'll have to manually set this up each time you want to debug and then remember to revert it back before committing any changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Use Overmind
&lt;/h4&gt;

&lt;p&gt;Another solution is to use a different tool to drive the &lt;code&gt;Procfile&lt;/code&gt;. The one I'm most familiar with is a tool called &lt;a href="https://github.com/DarthSim/overmind" rel="noopener noreferrer"&gt;overmind&lt;/a&gt;. If you run your &lt;code&gt;Procfile&lt;/code&gt; with &lt;code&gt;overmind&lt;/code&gt;, you'll be able to open up a new terminal window and individually connect to any of the processes that are running. So if you want to connect to the web process to debug, you can open up a new window and run &lt;code&gt;overmind connect web&lt;/code&gt;, and you'll have a window where you can work with the debugger's prompt.&lt;/p&gt;

&lt;p&gt;The downside of &lt;code&gt;overmind&lt;/code&gt; is that it requires &lt;a href="https://github.com/tmux/tmux" rel="noopener noreferrer"&gt;tmux&lt;/a&gt;, which is a terminal multiplexer tool. If you don't already use tmux, I'd say it's probably not worth learning it just for the purposes of using &lt;code&gt;overmind&lt;/code&gt;. But if you're like me and already know/use tmux, this can be a great solution to pursue.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Connect to debugger remotely
&lt;/h4&gt;

&lt;p&gt;In the &lt;a href="https://github.com/rails/cssbundling-rails/blob/main/lib/install/Procfile_for_node.dev" rel="noopener noreferrer"&gt;&lt;code&gt;Procfile.dev&lt;/code&gt;&lt;/a&gt; that's currently generated by the cssbundling-rails and jsbundling-rails gems, we can see that they write the web process like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

web: &lt;span class="nb"&gt;env &lt;/span&gt;&lt;span class="nv"&gt;RUBY_DEBUG_OPEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true &lt;/span&gt;bin/rails server


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This &lt;code&gt;RUBY_DEBUG_OPEN=true&lt;/code&gt; bit can allow us to remotely connect a debugger to the server instance the &lt;code&gt;Procfile&lt;/code&gt; is running. When you run the &lt;code&gt;Procfile&lt;/code&gt;, you'll see something like this in the server logs:&lt;/p&gt;

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

web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/run/user/1000/ruby-debug-josh-1396004)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Except probably with a different username than "josh" 🤣&lt;/p&gt;

&lt;p&gt;But like the message said, a debugger can attach remotely via UDS. Don't worry, it sounds more intimidating than it is. You just need to open a new terminal window and run &lt;code&gt;rdbg -An&lt;/code&gt;. The &lt;code&gt;-A&lt;/code&gt; option means "attach", and it automatically connects the debugger to the open UNIX domain socket that was setup. If you have multiple sockets open for a debugger to connect to (ie. because you're running multiple Rails projects), you may be forced to specify which one it should use. The &lt;code&gt;-n&lt;/code&gt; option means "nonstop." This prevents the program from stopping when you connect the debugger, which is usually what you're going to want when debugging a Rails app.&lt;/p&gt;

&lt;p&gt;Now when you hit a breakpoint, you'll be able to use the debugger in the terminal window where you remotely connected with &lt;code&gt;rdbg -An&lt;/code&gt;. This is straightforward to use and the method I recommend for most people. If it's a project where the &lt;code&gt;Procfile.dev&lt;/code&gt; doesn't provide that setup and you can't add it in, you can prefix the command to start your app with &lt;code&gt;RUBY_DEBUG_OPEN=true&lt;/code&gt; to still enable the same effect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Using the debugger to figure out tricky problems has saved me a ton of time when working on Rails apps. It's a great skill to learn and practice, but I also know that a lot of beginners avoid it because of how troublesome the setup can be. Hopefully this post has given you some ideas about the workflow you want to use for debugging 🐛&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>debugging</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Debugging Silent Create Action Failures in Rails</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Thu, 09 Nov 2023 18:27:07 +0000</pubDate>
      <link>https://dev.to/joshdevhub/debugging-silent-create-action-failures-in-rails-1boo</link>
      <guid>https://dev.to/joshdevhub/debugging-silent-create-action-failures-in-rails-1boo</guid>
      <description>&lt;p&gt;I often work with beginner Rails developers through &lt;a href="https://theodinproject.com"&gt;The Odin Project&lt;/a&gt; and &lt;a href="https://agencyoflearning.com/"&gt;The Agency of Learning&lt;/a&gt;. One common pain point people may run into while learning is the dreaded "silent create action" failure. You've written your model, controller, and routes for a new resource, you've built the form view for creating this resource, but when you fill out the form and click the submit button, nothing happens. And the logs may not offer obvious clues to pinpoint the problem.&lt;/p&gt;

&lt;p&gt;In this post, I'll discuss some techniques I find useful for getting to the bottom of this type of issue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolating Layers
&lt;/h2&gt;

&lt;p&gt;So let's say you have a typical &lt;code&gt;#create&lt;/code&gt; action 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;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="vi"&gt;@post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;post_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s1"&gt;'Post successfully saved'&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :unprocessable_entity&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You fill in the form for a new post, click submit, and then nothing happens. Where should you start? When you're filling in a form to create a resource, you're exercising every layer of the app to make that action happen: the view, the controller, and the model. It'd be a simpler problem if we could examine just one slice of the app. Narrowing down the focus makes it easier to spot bugs.&lt;/p&gt;

&lt;p&gt;So I begin by ensuring the model layer works in isolation. Rails provides a super convenient way to check this: the Rails console. The Rails console is a powerful tool for interacting with your application's code and data. Open up a rails console by doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bin/rails console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then try to get a &lt;code&gt;Post&lt;/code&gt; (or whatever resource you're working with) to save. Often you can spot problems with this related to how your data is set up, particularly when factoring in associations. In the above example, perhaps I've said that a &lt;code&gt;Post&lt;/code&gt; &lt;code&gt;belongs_to&lt;/code&gt; a &lt;code&gt;User&lt;/code&gt;. But because I haven't thought to provide a user id for the new post, the database will reject it.&lt;/p&gt;

&lt;p&gt;Most of the time, I take this step before even writing any route, controller, or view code at all. I like to make sure there are no problems with how I've set up associations and data migrations, and I like to have it fresh in my head exactly what data this model needs to save. But in the event that I didn't take this step when starting out, I make it the first thing I check when I run into trouble.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking on the Parameters
&lt;/h2&gt;

&lt;p&gt;A quick next step I take is to look at what the parameters are on the request to the &lt;code&gt;create&lt;/code&gt; action. You can find these by looking back through the server logs. They'll appear adjacent to where the logs start for the request like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;| Started POST "/posts" for 127.0.0.1 at &amp;lt;timestamp&amp;gt;
| Processing by PostsController#create
|   Parameters: {"authenticity_token"=&amp;gt;"[FILTERED]", "post"=&amp;gt;{"title"=&amp;gt;"Title", "body"=&amp;gt;"Post's body"}, "commit"=&amp;gt;"Create Post"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have trouble finding it among the other stuff happening in the server log, well, so do I! I recommend learning how to programmatically search through your terminal output. Providing a universal method for this is challenging because various tools and terminal emulators implement this functionality differently. Another option would be to use tools like &lt;code&gt;grep&lt;/code&gt; or &lt;a href="https://github.com/ggreer/the_silver_searcher"&gt;the_silver_searcher&lt;/a&gt; (a favorite of mine) to search the file where your dev logs are written to. This file is located at &lt;code&gt;log/development.log&lt;/code&gt; in a Rails project.&lt;/p&gt;

&lt;p&gt;After you've located the parameters, you can examine them for clues regarding the failure to create. You can see the exact data that your resource will be receiving, and you can consider if that data is what you expect. The parameters will also have a special annotation if any of them are "unpermitted". This happens when you haven't properly configured &lt;a href="https://guides.rubyonrails.org/action_controller_overview.html#strong-parameters"&gt;strong parameters&lt;/a&gt; for your resource.&lt;/p&gt;

&lt;h2&gt;
  
  
  Save with a Bang
&lt;/h2&gt;

&lt;p&gt;If I still don't know what the problem is, my next step is to change how I'm saving the object. The standard construction of a &lt;code&gt;save&lt;/code&gt; tends to look like:&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;if&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;
  &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="ss"&gt;:post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;notice: &lt;/span&gt;&lt;span class="s1"&gt;'Post successfully saved'&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:new&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;status: :unprocessable_entity&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, if the model fails to persist to the database, the &lt;code&gt;save&lt;/code&gt; call will return &lt;code&gt;false&lt;/code&gt;. This will fire off our &lt;code&gt;else&lt;/code&gt; condition. This is nice for elegantly handling issues with user inputs, but it's less nice for understanding any bug that is causing the save's failure. We need to get at what's going wrong rather than just silently rendering the &lt;code&gt;new&lt;/code&gt; template for this resource.&lt;/p&gt;

&lt;p&gt;To get more information about what exactly is causing the failure, I like to temporarily change my &lt;code&gt;#save&lt;/code&gt; to a &lt;code&gt;#save!&lt;/code&gt;. When you add a &lt;code&gt;!&lt;/code&gt;, a failure no longer returns &lt;code&gt;false&lt;/code&gt;. Instead, it will raise an error. Errors and their messages tend to provide crucial insights into the underlying cause of a problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Big Guns
&lt;/h2&gt;

&lt;p&gt;For a straightforward &lt;code&gt;#create&lt;/code&gt; action, I usually find the steps above are sufficient for solving the problem. Sometimes there's more going on in the action though. If I still don't have a great understanding of the problem, I'll usually break out the ultimate tool: a debugger. &lt;/p&gt;

&lt;p&gt;Debuggers are powerful tools that allow you to step through your code line-by-line, inspecting variables and understanding the flow of execution. Using debuggers is a whole topic unto itself, and getting into the weeds with that would balloon the scope of this post. If you want more information on using them, I recommend reading the &lt;a href="https://github.com/ruby/debug"&gt;README for rdbg&lt;/a&gt;. This is &lt;em&gt;the&lt;/em&gt; debugging solution for modern Ruby/Rails development. It's in Ruby's stdlib as of v3.1, and Rails 7+ apps include it in the Gemfile by default. I also recommend &lt;a href="https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem"&gt;this section of the Rails guides&lt;/a&gt; for exploring how to use the debug gem with Rails applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Developing a Process
&lt;/h2&gt;

&lt;p&gt;Hopefully the steps I've outlined here can help demystify these silent &lt;code&gt;create&lt;/code&gt; failures. I've found it helpful to develop a debugging  process for the different types of common errors I run into, and I encourage other developers do the same. Remember, debugging isn't just about fixing errors; it's a skill that evolves with each challenge. Embrace the journey, uncover the intricacies of your code, build a debugging process. You'll understand your code better, and you'll solve errors faster.&lt;/p&gt;

&lt;p&gt;Happy debugging 🐛&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>debugging</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Emmet Snippets in Rails Views</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Thu, 07 Sep 2023 18:41:16 +0000</pubDate>
      <link>https://dev.to/joshdevhub/emmet-snippets-in-rails-views-5c5a</link>
      <guid>https://dev.to/joshdevhub/emmet-snippets-in-rails-views-5c5a</guid>
      <description>&lt;p&gt;&lt;a href="https://emmet.io/" rel="noopener noreferrer"&gt;Emmet&lt;/a&gt; is a tool that helps you rapidly build sections of HTML by typing abbreviated snippets of what you want. If you're using VSCode, you can give it a quick test drive by opening a &lt;code&gt;.html&lt;/code&gt; file and trying this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- type this and press `tab` in a `.html` file --&amp;gt;&lt;/span&gt;
div&amp;gt;ul&amp;gt;li.list-item*3

&lt;span class="c"&gt;&amp;lt;!-- and you'll see it expand to: --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"list-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"list-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"list-item"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Support for this is built in for the current most popular editor: &lt;a href="https://code.visualstudio.com/docs/editor/emmet" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt;. But if you work with Rails a lot, you'll probably notice that it does not work in &lt;code&gt;erb&lt;/code&gt; template files out of the box. Let's enable it!&lt;/p&gt;

&lt;p&gt;To start, open your &lt;code&gt;settings.json&lt;/code&gt; file in VSCode. You can take a shortcut there by opening the command palette (&lt;code&gt;ctrl/cmd + shift + p&lt;/code&gt;) and typing "settings.json". Look for the item &lt;code&gt;Open User Settings (JSON)&lt;/code&gt; and click it to open the file. Inside the curly braces of the JSON, you'll want to add this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"emmet.includeLanguages"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"html.erb"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"html"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will tell Emmet to include its HTML snippets for files with the &lt;code&gt;.html.erb&lt;/code&gt; extension. Save and close your &lt;code&gt;settings.json&lt;/code&gt;, open up a view template in a Rails project, and you should now be able to use these snippets. &lt;a href="https://docs.emmet.io/cheat-sheet/" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is an awesome cheatsheet that shows some of the cool stuff you can do to generate HTML quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap with Abbreviation
&lt;/h2&gt;

&lt;p&gt;The snippet stuff can be super useful, but I think one of the best Emmet tricks is something that isn't enabled by default in VSCode. Have you ever needed to wrap a section of HTML in a &lt;code&gt;div&lt;/code&gt; for styling purposes? I'm terrible at estimating how many containers I need for styling, so I have to do this often. But I find the manual flow for this cumbersome. You have to type the opening tag, the closing tag on a completely different line while still matching the opening tag's indentation, and then you have to indent the section between the tags you've added. To help with this, we can use Emmet's &lt;code&gt;wrap with abbreviation&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbogb0l6t3lg9n9mmpbyw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbogb0l6t3lg9n9mmpbyw.gif" alt="gif demonstrating wrap with abbreviation with emmet"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So you can highlight the section you want to wrap, hit a keybind, and then type the emmet snippet you want to wrap that section in. Emmet will handle inserting the tags and indenting the content in between automatically.&lt;/p&gt;

&lt;p&gt;VSCode doesn't have this keybind set by default. To set it, you'll need to navigate to your keyboard shortcuts. Click the gear icon in the bottom left and you should see &lt;code&gt;Keyboard Shortcuts&lt;/code&gt; in the submenu. Click on it and then search 'wrap with abbreviation'. You should see the Emmet command pop up, and you'll be able to set the keybind to something that's comfortable to use for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Considerations
&lt;/h2&gt;

&lt;p&gt;What if you don't use VSCode? That's understandable, my preferred editor is Neovim. Emmet is still probably supported in some fashion by the editor you use. You'll just have to google to fill in some gaps. I wrote this for VSCode because it's what 95% of developers use these days.&lt;/p&gt;

&lt;p&gt;You also might sometimes want these snippets in other places, like in &lt;code&gt;.jsx&lt;/code&gt; files if you work with React. In that case, you can add &lt;code&gt;.jsx&lt;/code&gt; to &lt;code&gt;emmet.includeLanguages&lt;/code&gt; in your &lt;code&gt;settings.json&lt;/code&gt; file like I showed with &lt;code&gt;html.erb&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>html</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Debugging Flaky Tests in CI</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Thu, 13 Jul 2023 22:26:02 +0000</pubDate>
      <link>https://dev.to/joshdevhub/debugging-flaky-tests-in-ci-b43</link>
      <guid>https://dev.to/joshdevhub/debugging-flaky-tests-in-ci-b43</guid>
      <description>&lt;h1&gt;
  
  
  Flaky Tests
&lt;/h1&gt;

&lt;p&gt;Debugging CI test failures can be frustrating. Sometimes it's impossible to recreate the failure conditions locally. We recently had an issue with some our tests flaking in CI at my job. The experience of debugging it made me want to write a small post on the process. I hope it can help others encountering these types of problems 💪&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;At my job, we use Minitest as our testing framework for our Ruby on Rails application. We have simple GitHub actions for running continuous integration (CI). We noticed a few of the tests were occasionally failing in the CI run for Minitest. The frequency of the failures seemed to be roughly 2/3 pass to fail. Every failing test had a few things in common. They were all tests for our API controllers, they were all for a &lt;code&gt;#create&lt;/code&gt; action, and they were all following this general pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="s2"&gt;"create new resource"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;assert_difference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Resource.count'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s1"&gt;'route'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;params: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="ss"&gt;resource: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;attrs&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;access_token: &lt;/span&gt;&lt;span class="n"&gt;access_token&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="ss"&gt;as: :json&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The goal here is that when a user POSTs to that route with valid attributes and access token, a new resource is persisted to the test database. The &lt;code&gt;assert_difference('Resource.count')&lt;/code&gt; checks this by looking for if the count increases by 1.&lt;/p&gt;

&lt;p&gt;The failure message was a simple &lt;code&gt;"Resource.count" didn't change by 1&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Process
&lt;/h2&gt;

&lt;p&gt;The main thing I try to do when debugging is recreate the situation that produces the bug. If I can guarantee a situation that produces the bug consistently, I can cut out the noise and focus in on what's going wrong. Sometimes this is straightforward, but with problems in CI, that's often not the case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Idea 1: Pull the seed number of the failing CI test
&lt;/h3&gt;

&lt;p&gt;Testing frameworks usually offer a mechanism for running the tests in a random order, and Minitest is no exception. This is useful because it's important that tests are free of side effects. If TestA only passes when run after TestB -- this is a problem, and TestA isn't testing what it claims to be testing. The randomized runs help check for these types of situations. Alongside a randomized run, Minitest will output a "seed" identifier for that test run. This allows developers to run the tests in that specific order again if they want to.&lt;/p&gt;

&lt;p&gt;Our first idea was to investigate the log output for the failing GitHub Action. These logs will show the seed value used in the test run. If we pass that seed identifier to Minitest locally, will the tests fail in our dev environment?&lt;/p&gt;

&lt;p&gt;They continued to pass in development ... On to the next idea!&lt;/p&gt;

&lt;h3&gt;
  
  
  Idea 2: Force the GitHub action to run with a failing seed every time
&lt;/h3&gt;

&lt;p&gt;So we've confirmed the bug only exists in the CI environment. My coworker had the idea to take a failing seed number and add it to the Minitest command in the workflow for our GitHub testing action:&lt;br&gt;
&lt;code&gt;run: bundle exec rails test --seed=${FAILING_SEED_NUMBER}&lt;/code&gt;&lt;br&gt;
Then we could examine if the test fails in CI every time rather than only occasionally. After a few runs, we felt confident that the test was consistently failing in CI. It's inconvenient weren't able to recreate the problem locally. But at least we had a consistent environment for the failures.  Now we can begin examining the failing cases.&lt;/p&gt;
&lt;h3&gt;
  
  
  Print Debugging
&lt;/h3&gt;

&lt;p&gt;Unfortunately we can't hook up a debug session for the tests running in CI. So we had to return to our old friend: print debugging. We spammed a few &lt;code&gt;p&lt;/code&gt; and &lt;code&gt;puts&lt;/code&gt; statements in our failing test cases. What were the values of different local variables? What was &lt;code&gt;'Resource.count'&lt;/code&gt; returning? What were the instance variables configured in our &lt;code&gt;setup&lt;/code&gt; method returning? And finally, what did &lt;code&gt;response&lt;/code&gt; look like?&lt;/p&gt;

&lt;p&gt;We quickly found out that this wasn't failing because of a mismatch in the output and assertion. Instead it was failing because of an error raised during the test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ActiveRecord::RecordNotUnique (duplicate key value violates unique constraint). Key already exists.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After examining the code, we became suspicious of a FactoryBot method in our test's &lt;code&gt;#setup&lt;/code&gt;:&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;setup&lt;/span&gt;
  &lt;span class="c1"&gt;# access_token and user configuration&lt;/span&gt;
  &lt;span class="vi"&gt;@resource&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;attr1: &lt;/span&gt;&lt;span class="s2"&gt;"attr1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;attr2: &lt;/span&gt;&lt;span class="s2"&gt;"attr2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;id: &lt;/span&gt;&lt;span class="s1"&gt;'1'&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;An ID was explicitly assigned in the factory. This id was being used for assertions in a different test cases for the controller. After a little research, it wasn't too difficult to find examples of this kind of thing being a problem (see: &lt;a href="https://stackoverflow.com/questions/61380668/factorybot-unique-constraint-violation-error-when-specifying-id-in-the-factory"&gt;here&lt;/a&gt;). By directly assigning an ID, the natural incrementing of primary keys can be thrown off. So we decided to remove that id argument and change the assertions that relied on the id to look for &lt;code&gt;@resource.id&lt;/code&gt; instead. We made these changes for each test case that was using this pattern.&lt;/p&gt;

&lt;p&gt;Then we pushed the code up with the CI test command still set to run the failing seed, and it passed! We then went back to running the GitHub action without a specified seed so it would return to running in a random order. After a several successful runs in a row, we felt confident that we'd fixed the problem!&lt;/p&gt;

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

&lt;p&gt;I've run into these types of CI issues a few times now through my job and in open source projects. It can be a difficult when you can't recreate the failures in development. But a little patience and an organized debugging process can make quick work of the problem ✅&lt;/p&gt;

&lt;h3&gt;
  
  
  Further reading:
&lt;/h3&gt;

&lt;p&gt;I got a lot out of these two flaky test articles by Jason Swett:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.codewithjason.com/what-causes-flaky-tests/"&gt;What causes flaky tests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.codewithjason.com/how-i-fix-flaky-tests/"&gt;How I fix flaky tests&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>rails</category>
      <category>testing</category>
      <category>ci</category>
    </item>
    <item>
      <title>Terminal Colors Using Ruby</title>
      <dc:creator>Josh Smith</dc:creator>
      <pubDate>Fri, 23 Sep 2022 02:55:29 +0000</pubDate>
      <link>https://dev.to/joshdevhub/terminal-colors-using-ruby-410p</link>
      <guid>https://dev.to/joshdevhub/terminal-colors-using-ruby-410p</guid>
      <description>&lt;p&gt;I'm a web development student who's been learning through &lt;a href="https://theodinproject.com" rel="noopener noreferrer"&gt;The Odin Project&lt;/a&gt; for the past year now (wow time flies). Students who join me in the &lt;a href="https://www.theodinproject.com/paths/full-stack-ruby-on-rails/courses/ruby-programming" rel="noopener noreferrer"&gt;Ruby on Rails&lt;/a&gt; course will encounter a number of projects where we build terminal applications using plain ol' Ruby. This is great for developing general programming skills and familiarity with the Ruby language, but since you're in the terminal, it can be a challenge to make your apps aesthetically pleasing. In this post, I'll explore some ways you can add visual flare to your ruby terminal apps through using colors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Off-the-Shelf Solutions
&lt;/h2&gt;

&lt;p&gt;So you want to add color to your terminal app? Since you're a programmer, the first thing you do is go straight to Google. This generally leads people to two ideas. One is the popular &lt;a href="https://rubygems.org/gems/colorize" rel="noopener noreferrer"&gt;Colorize&lt;/a&gt; gem, which is an easy and super convenient way to get color functionality into your app. You just install the gem, &lt;code&gt;require&lt;/code&gt; it into whatever file(s) you wish to use it in, and then you'll have several color utility functions added onto the &lt;code&gt;String&lt;/code&gt; class. Other things people often run into are Stack Overflow posts like &lt;a href="https://stackoverflow.com/questions/1489183/how-can-i-use-ruby-to-colorize-the-text-output-to-a-terminal" rel="noopener noreferrer"&gt;this one&lt;/a&gt;, which essentially peel back the curtain on how the Colorize gem works. When we want to add colors to a string, we wrap the string in an &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code" rel="noopener noreferrer"&gt;ANSI escape code&lt;/a&gt; that signals how your terminal emulator should style the text.&lt;/p&gt;

&lt;p&gt;Both of these methods of injecting color into your app are great and easy to use. I would say they're ideal for if you're just wanting to color the log output for some server or process you're running. But if you're using them to add color to a terminal app or game, I find these and most other solutions out there underwhelming for one big reason: they offer an extremely limited color palette. The Colorize gem defines methods for 16 colors, and most other sources only show how to work with 8 or 16 colors. But most modern terminal emulators supports 24-bit "true color", meaning we actually have access to the RGB spectrum of 16,777,216 colors. Using this full spectrum can lend a lot more flavor and personality to a terminal app. So let's explore how to do it!&lt;/p&gt;

&lt;h2&gt;
  
  
  A Note about MacOS Terminal
&lt;/h2&gt;

&lt;p&gt;The default MacOS Terminal app would be one of the modern terminals that does not support true color. If you're a Mac user and want to use the exact technique I show in this post, you'll have to use a different terminal emulator. iTerm2 is a popular choice on MacOS. It provides support for true color and a few other cool features not present in the default Terminal app.&lt;/p&gt;

&lt;p&gt;Another option would be to use 256 color mode, which will work on the default Terminal app. For this mode, you can still reference the rest of the article except your ANSI color string will look like this:&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;# the 5 at the beginning puts this in 256 color mode&lt;/span&gt;
&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[38;5;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;color_code&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;m&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And you can find what colors map to what color code through this &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit" rel="noopener noreferrer"&gt;table on wikipedia&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  24-bit ANSI color code
&lt;/h2&gt;

&lt;p&gt;So above I mentioned that ANSI codes are used to style the text in a terminal. They can also be used for many other things from moving the cursor around to deleting lines of text, but right now I'll just be focusing on the colors. We can see the form of the 24-bit ANSI code &lt;a href="https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Translated into a Ruby string, it would look this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[38;2;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="si"&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;g&lt;/span&gt;&lt;span class="si"&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;b&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;m&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's break down what's happening here. &lt;code&gt;"\e["&lt;/code&gt; is the "control sequence introducer" and is how we begin using one of these codes. This is followed by a list of semicolon-separated parameter values to communicate what exactly should happen. &lt;code&gt;38&lt;/code&gt; signals that we're setting the foreground color (we'd use &lt;code&gt;48&lt;/code&gt; for the background). The &lt;code&gt;2&lt;/code&gt; signals that we're using the RGB format for the color. Then we list each RGB value for the color we want to use. The &lt;code&gt;'m'&lt;/code&gt; is a "final byte" to signal the end of the parameter list. This is followed by the string we want to apply the color to. The final piece is the &lt;code&gt;"\e[0m"&lt;/code&gt; reset code. This returns the colors to their default state.&lt;/p&gt;

&lt;p&gt;So let's quickly test this is working using the color green (r: 0, g: 128, b: 0).&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;green_rgb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"0;128;0"&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[38;2;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;green_rgb&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;mhello&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m world"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And you should see this text when you run the above code: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk9lp86gjaw7u2ydzo3rb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk9lp86gjaw7u2ydzo3rb.png" alt="Demonstration of the above ANSI code terminal output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is good. We have our green "hello", and "world", which was placed after the reset code, is back to normal.&lt;/p&gt;

&lt;p&gt;So what's a good way to go about building this into our app? You can probably already see a method that can arise from the above code. Perhaps something like:&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;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rgb_values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[38;2;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;rgb_values&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;m&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m"&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;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"0;128;0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But I think it'd be nice for the caller to not have to know or remember these RGB values, and if we extend the &lt;code&gt;String&lt;/code&gt; class like the other solutions above do, we can get really clean, chainable methods. So let's explore that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building a Color Module
&lt;/h2&gt;

&lt;p&gt;So I'm going to build a module that gives the &lt;code&gt;String&lt;/code&gt; class some color utilities, but first I want to mention the pitfalls of "monkey patching" (or extending the base classes of the language). Ruby makes this very simple to do. We can just reopen any of the base classes anytime we like.&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;String&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!!!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
&lt;span class="c1"&gt;#-&amp;gt; "hello world!!!"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;But despite the ease of doing this, it's often considered a code smell. One of the reasons is that this kind of code will be globally available -- because &lt;code&gt;String&lt;/code&gt; itself is of course globally available. You may think that this kind of thing could work&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;StringExtensions&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;
    &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!!!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;
  &lt;span class="no"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include&lt;/span&gt; &lt;span class="no"&gt;StringExtensions&lt;/span&gt;
  &lt;span class="c1"&gt;# these methods are only available in this class, right?&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;yell_at_user&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Don't do that"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
&lt;span class="c1"&gt;#-&amp;gt; "hello world!!!"&lt;/span&gt;
&lt;span class="c1"&gt;# the #shout method is available anywhere&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We'd rather these methods only be available where they can/should be used. Enter the &lt;code&gt;refine&lt;/code&gt; and &lt;code&gt;using&lt;/code&gt; methods, which are builtin methods on the &lt;code&gt;Module&lt;/code&gt; class. With &lt;code&gt;refine&lt;/code&gt;, we can define some extensions on a base class, and then with &lt;code&gt;using&lt;/code&gt;, we can be selective with where these refinements are available in our code. Check it out:&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;StringExtensions&lt;/span&gt;
  &lt;span class="n"&gt;refine&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;shout&lt;/span&gt;
      &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!!!"&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;
  &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="no"&gt;StringExtensions&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;yell_at_user&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Don't do that"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;yell_at_user&lt;/span&gt;
&lt;span class="c1"&gt;#-&amp;gt; "Don't do that!!!"&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"hello world"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;shout&lt;/span&gt;
&lt;span class="c1"&gt;#-&amp;gt; NoMethodError&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using these methods, we can better control where the extensions to a base class are available. With this in mind, let's build a small color module. I'll use a few colors from the scheme of my favorite editor theme: &lt;a href="https://draculatheme.com/contribute" rel="noopener noreferrer"&gt;dracula&lt;/a&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;ColorableString&lt;/span&gt;
  &lt;span class="no"&gt;RGB_COLOR_MAP&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="ss"&gt;cyan: &lt;/span&gt;&lt;span class="s2"&gt;"139;233;253"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;green: &lt;/span&gt;&lt;span class="s2"&gt;"80;250;123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="ss"&gt;red: &lt;/span&gt;&lt;span class="s2"&gt;"255;85;85"&lt;/span&gt;
  &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;freeze&lt;/span&gt;

  &lt;span class="n"&gt;refine&lt;/span&gt; &lt;span class="no"&gt;String&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;rgb_val&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RGB_COLOR_MAP&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;color_name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[38;2;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;rgb_val&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;m&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="se"&gt;\e&lt;/span&gt;&lt;span class="s2"&gt;[0m"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Display&lt;/span&gt;
  &lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="no"&gt;ColorableString&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;welcome_user&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Welcome to my App"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:green&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;user_choice&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Please choose an option"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:cyan&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;yell_at_user&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Don't do that"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fg_color&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:red&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="nb"&gt;display&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;

&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;welcome_user&lt;/span&gt;
&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;user_choice&lt;/span&gt;
&lt;span class="nb"&gt;display&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;yell_at_user&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4jt0g3h44ysqngr2i6c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4jt0g3h44ysqngr2i6c.png" alt="Demonstration of above terminal output from above code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now we have color! And it's in a format that's easy to reuse and extend. If you need background colors, just create a &lt;code&gt;bg_color&lt;/code&gt; method with the &lt;code&gt;38&lt;/code&gt; replaced by a &lt;code&gt;48&lt;/code&gt;. You can also create the color methods through metaprogramming if you feel so inclined, but I felt it best to keep it simple. Happy colorful coding!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
