Jul 15, 2025 rails · console · development · ruby

Custom Colorized Environment String in Rails Console

<p>Ever opened a Rails console and wondered &ldquo;wait, which environment am I in again?&rdquo; Yeah, me too. Especially when you&rsquo;re switching between development, staging, and production faster than you can say <code>User.destroy_all</code>.</p> <p>Here&rsquo;s a neat trick to add some color to your console prompt so you never have to wonder again.</p> <h2>The Problem</h2> <p>The default Rails console visual feedback stops being useful because staging environments are usually configured as &ldquo;production&rdquo; on Heroku and most deployment platforms. So <code>Rails.env.production?</code> returns true for both staging and production. Fun times!</p> <h2>The Solution</h2> <p>Drop this into <code>config/initializers/console.rb</code>:</p> <div class="highlight"><pre class="highlight ruby"><code><span class="c1"># frozen_string_literal: true</span> <span class="nb">require</span> <span class="s2">"rails/commands/console/irb_console"</span> <span class="k">module</span> <span class="nn">IRBConsoleWithDeployEnv</span> <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span> <span class="k">super</span><span class="p">(</span><span class="n">app</span><span class="p">)</span> <span class="vi">@deploy_env</span> <span class="o">=</span> <span class="n">env</span> <span class="k">end</span> <span class="k">def</span> <span class="nf">colorized_env</span> <span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="p">.</span><span class="nf">const_set</span><span class="p">(</span><span class="ss">:DIM</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="k">unless</span> <span class="k">defined?</span><span class="p">(</span><span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="o">::</span><span class="no">DIM</span><span class="p">)</span> <span class="k">return</span> <span class="k">super</span> <span class="k">unless</span> <span class="vi">@deploy_env</span> <span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="p">.</span><span class="nf">colorize</span><span class="p">(</span><span class="s2">"live:"</span><span class="p">,</span> <span class="p">[</span><span class="ss">:DIM</span><span class="p">])</span> <span class="o">+</span> <span class="k">case</span> <span class="vi">@deploy_env</span> <span class="k">when</span> <span class="s2">"staging"</span> <span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="p">.</span><span class="nf">colorize</span><span class="p">(</span><span class="s2">"stag"</span><span class="p">,</span> <span class="p">[</span><span class="ss">:YELLOW</span><span class="p">])</span> <span class="k">when</span> <span class="s2">"production"</span> <span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="p">.</span><span class="nf">colorize</span><span class="p">(</span><span class="s2">"prod"</span><span class="p">,</span> <span class="p">[</span><span class="ss">:RED</span><span class="p">])</span> <span class="k">else</span> <span class="no">IRB</span><span class="o">::</span><span class="no">Color</span><span class="p">.</span><span class="nf">colorize</span><span class="p">(</span><span class="vi">@deploy_env</span><span class="p">,</span> <span class="p">[</span><span class="ss">:BLUE</span><span class="p">])</span> <span class="k">end</span> <span class="k">end</span> <span class="no">Rails</span><span class="o">::</span><span class="no">Console</span><span class="o">::</span><span class="no">IRBConsole</span><span class="p">.</span><span class="nf">prepend</span> <span class="nb">self</span> <span class="k">end</span> </code></pre></div> <p>And this into <code>config/application.rb</code>:</p> <div class="highlight"><pre class="highlight ruby"><code><span class="k">module</span> <span class="nn">MyApp</span> <span class="k">class</span> <span class="nc">Application</span> <span class="o">&lt;</span> <span class="no">Rails</span><span class="o">::</span><span class="no">Application</span> <span class="c1"># Other configurations...</span> <span class="n">console</span> <span class="k">do</span> <span class="o">|</span><span class="n">app</span><span class="o">|</span> <span class="n">config</span><span class="p">.</span><span class="nf">console</span> <span class="o">=</span> <span class="no">Rails</span><span class="o">::</span><span class="no">Console</span><span class="o">::</span><span class="no">IRBConsole</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">app</span><span class="p">,</span> <span class="no">ENV</span><span class="p">[</span><span class="s2">"DEPLOY_ENV"</span><span class="p">])</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span> </code></pre></div> <p>Now your console prompt will show a nice colorized environment string prefixed by your app name. No more &ldquo;oops, wrong environment&rdquo; moments.</p>
Feb 16, 2022 git · development · pairing

Add Co-Authored-By to all commits after pairing

<h3>TL;DR</h3> <p>Here&rsquo;s the full solution:</p> <div class="highlight"><pre class="highlight shell"><code>git filter-branch <span class="nt">--msg-filter</span> <span class="s2">"ruby -e'puts [</span><span class="nv">$stdin</span><span class="s2">.read,nil,*ARGV].join(%{</span><span class="se">\\</span><span class="s2">n})' -- 'Co-Authored-By: John Doe &lt;[email protected]&gt;'"</span> origin/master..HEAD </code></pre></div> <p>Now let&rsquo;s break it down, piece by piece.</p> <h2>The commits</h2> <p>This will list all commits that are in your branch but not on <code>origin/master</code></p> <div class="highlight"><pre class="highlight shell"><code>origin/master..HEAD </code></pre></div> <h2>The filter</h2> <p>The simplest ruby script that will:</p> <ol> <li>read the original commit message from <code>$stdin</code></li> <li>append any provided argument with a new line</li> </ol> <div class="highlight"><pre class="highlight ruby"><code><span class="nb">puts</span> <span class="p">[</span> <span class="vg">$stdin</span><span class="p">.</span><span class="nf">read</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="c1"># extra empty line, for good measure</span> <span class="o">*</span><span class="no">ARGV</span> <span class="p">].</span><span class="nf">join</span><span class="p">(</span><span class="sx">%{\n}</span><span class="p">)</span> </code></pre></div> <p>And we use <code>--</code> to separate file names to execute from CLI arguments:</p> <div class="highlight"><pre class="highlight shell"><code><span class="nv">$ </span>ruby <span class="nt">-e</span> <span class="s1">'p ARGV'</span> <span class="nt">--</span> <span class="s2">"anything after '--'"</span> <span class="s2">"ends up in ARGV"</span> <span class="o">[</span><span class="s2">"anything after '--'"</span>, <span class="s2">"ends up in ARGV"</span><span class="o">]</span> </code></pre></div> <h2>Putting it all together</h2> <p>We&rsquo;re lucky <code>git filter-branch</code> has an option for changing commit messages, we&rsquo;ll use that to append <code>Co-Authored-By: …</code> information to every commit in the current branch.</p> <p>The <code>--msg-filter</code> option will execute a script that will take the old message as its standard input and use the standard output as the new message.</p> <div class="highlight"><pre class="highlight shell"><code>git filter-branch <span class="nt">--msg-filter</span> <span class="s2">"ruby -e'puts [</span><span class="nv">$stdin</span><span class="s2">.read,nil,*ARGV].join(%{</span><span class="se">\\</span><span class="s2">n})' -- 'Co-Authored-By: John Doe &lt;[email protected]&gt;'"</span> origin/master..HEAD </code></pre></div>

The git-rebase option no-one knows that will shock your coworkers!

<p>A few weeks ago, a moment before pushing upstream a glorious bug-fix, I ran the test suite locally, just to discover that I broke it!</p> <p>Having lost all confidence in the commit history, I was out for blood, looking for the piece of code that ruined the whole PR!</p> <p><div class="tenor-gif-embed" data-postid="14001328" data-share-method="host" data-width="100%" data-aspect-ratio="1.5323076923076924"><a href="https://tenor.com/view/survivor-38-survivor38-lauren-oconnell-out-for-blood-gif-14001328">Survivor 38 GIF</a> from <a href="https://tenor.com/search/survivor-gifs">Survivor GIFs</a></div><script type="text/javascript" async src="https://tenor.com/embed.js"></script></p> <p>With more than a hundred git aliases I consider myself quite a git nerd, so I reached out for a tool I knew: <code>git rebase</code>.</p> <p>My plan was:</p> <ol> <li>mark each commit with <code>edit</code></li> <li>run the test suite with <code>bin/rake</code> at each stop</li> <li>identify the offending commit and fix whatever bugs I encountered</li> <li>amend the commit and call it a day</li> </ol> <p>That didn&rsquo;t feel right tho, it was too clunky, I had a hunch that I could have done better than this! So, after reading once again the <code>git rebase</code> documentation, this little option popped out: <code>git rebase --exec</code>!</p> <h2>What <code>--exec</code> does to <code>git rebase</code></h2> <p>The magic of <code>--exec</code> is that it will execute a command after each commit in your interactive rebase. If the command fails, it will stop the rebase, allow you to fix whatever needs to be fixed, and move along with <code>git rebase --continue</code>.</p> <p>This means that I was able to swiftly run this command after each commit and ensure that I had a green suite at every step of the PR.</p> <div class="highlight"><pre class="highlight shell"><code>git rebase <span class="nt">--interactive</span> <span class="nt">--exec</span> <span class="s2">"bin/rake"</span> origin/master </code></pre></div> <p>Don&rsquo;t forget that this neat trick can be used for all kinds of problems, like, for example, running a linter for every commit of a PR.</p>
Nov 4, 2013 opal · ruby · euruko · lightning · talk · 2013

Euruko 2013 Opal lightining talk

<p> For those who missed it here's the lightning talk I gave in Athens at <a href="http://euruko2013.org">Euruko 2013</a> <del>(at minute 17:00)</del>: </p> <a href="http://cl.ly/0F203A093L2b" target="_blank"> <figure> <img src="/assets/euruko-talk-poster.jpg" alt="euruko-talk-poster"/> <figcaption>Downlaod it here</figcaption> </figure> </a> <!-- <iframe width="420" height="275" src="http://www.ustream.tv/embed/recorded/35065939/highlight/376933?v=3&amp;wmode=direct" scrolling="no" frameborder="0" style="border: 0px none transparent;"> </iframe> --> <p> <ul> <li><del><a href="http://www.ustream.tv/recorded/35065939/highlight/376933">Watch on Ustream</a></del></li> <li><a href="http://cl.ly/3w301C0F1535">The slides (html with embedded video, ~14MB)</a></li> </ul> <em>(beware: the slides in the video have the wrong aspect raito)</em> </p>
Oct 10, 2013 opal · ruby · browser · meh · tabs · spaces

Reading meh's code

<img src="/assets/jonathan-goldsmith-eyes.jpg" alt="jonathan-goldsmith-eyes"/> <blockquote> <p> I don't always read <a href="https://github.com/meh">meh's code</a>… <br> but when I do it, I convert tabs to spaces. </p> </blockquote> <p> The bookmarklet: <kbd> <a style="color:inherit;" href="javascript:e=document.querySelector('div.highlight%3Epre');e.innerHTML=e.innerHTML.replace(/(&nbsp;)+/g,function(s)%7Breturn%20new%20Array((s.length/6/4)+1).join('&nbsp;')%7D).replace(/%09/g,'&nbsp;&nbsp;')"> ↦ meh` </a> </kbd> <span class="hint">(drag it to your bookmark bar)</span> <br> <br> <strong>UPDATE:</strong> Now works with GitHub expanding tabs to non-breaking-spaces (<a href="https://gist.github.com/elia/7097560">history here</a>) </p>
Aug 21, 2013 opal · ruby · browser · talk · slides · video · rails · javascript · italian

Opal Talk at RubyDay 2013 video availble!

<p> Hey, the talk I gave at this year's (2013) RubyDay is now on youtube! <br> I'll update the post with the slides soon :) <br> <br> <strong>(LANGUAGE: Italian)</strong>. </p> <iframe width="420" height="315" src="//www.youtube.com/embed/O_H9E3VpSOc?rel=0" frameborder="0" allowfullscreen></iframe>
Jul 3, 2013 ruby · rails · rails3 · railsapi · sdoc · documentation · docs · ruby2 · github · api

Past and Future of Ruby and Rails

<p>As a sad orphan of railsapi.com I&#8217;m proud to present you valuable links to ruby and rails documentation in SDoc format:</p> <p><strong>Searchable Ruby 2.0 API documentation</strong></p> <p><a href="http://elia.github.io/railsapi.com/public/doc/ruby-v2.0/" target="_blank">http://elia.github.io/railsapi.com/public/doc/ruby-v2.0/</a></p> <p><strong>Searchable Rails 3.2 API documentation</strong></p> <p><a href="http://elia.github.io/railsapi.com/public/doc/rails-v3.2/" target="_blank">http://elia.github.io/railsapi.com/public/doc/rails-v3.2/</a></p>
Mar 4, 2013 git · autocomplete · shorthand · shortcuts · bash

Autocomplete "git" as "g"

<p>For all you lazy Git bums, here&#8217;s how you can type &#8220;g&#8221; got &#8220;git&#8221; and still have autocompletion (paste into your ~/.bash_profile or .dotfiles system):</p> <p><code></code></p> <pre> alias g='git' complete -o bashdefault -o default -o nospace -F _git g 2&gt;/dev/null \ || complete -o default -o nospace -F _git g </pre>
Feb 12, 2013 pow · jruby · hack · sockets · support

Let jRuby and Pow!™ be happy together

<p>Turns out that <a href="http://pow.cx" target="_blank">Pow!</a> <a href="https://github.com/josh/nack/pull/32#issuecomment-8476565" target="_blank">won&#8217;t support jRuby anytime soon</a> but the trick described in <a href="https://github.com/josh/nack/pull/32#issuecomment-8476565" target="_blank">josh/nack#32</a> actually works, so rollup your shirt&#8217;s sleeves!</p> <p>Go open <code>node_modules/nack/lib/nack/server.rb</code> inside <code>~/Library/Application Support/Pow/Current</code> and replace <code>sock.close_read</code> on line 132 with:</p> <pre class="ruby"> # WAS: sock.close_read begin sock.close_read rescue IOError raise unless RUBY_ENGINE == 'jruby' end</pre> <p>Happy hacking!</p>
Nov 29, 2012

rvm install opal

<img src="/tumblr_files/tumblr_me9r8yQVui1qzaszdo1_1280.png" alt="" />