<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mike Gregory on Medium]]></title>
        <description><![CDATA[Stories by Mike Gregory on Medium]]></description>
        <link>https://medium.com/@mgregory8219?source=rss-48b79948652d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*eOt7C_4dyDJbzwHZ.</url>
            <title>Stories by Mike Gregory on Medium</title>
            <link>https://medium.com/@mgregory8219?source=rss-48b79948652d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 00:27:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mgregory8219/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What I Learned Writing a Simple CLI in Crystal]]></title>
            <link>https://medium.com/@mgregory8219/what-i-learned-writing-a-simple-cli-in-crystal-28c577702083?source=rss-48b79948652d------2</link>
            <guid isPermaLink="false">https://medium.com/p/28c577702083</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[crystal-lang]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Mike Gregory]]></dc:creator>
            <pubDate>Tue, 17 Jul 2018 16:57:12 GMT</pubDate>
            <atom:updated>2023-01-11T19:58:36.024Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FgAyEr9XlVRGnZ4e" /><figcaption>“Man wearing headphones at desk with window view of sunset in background” by <a href="https://unsplash.com/@flysi3000?utm_source=medium&amp;utm_medium=referral">Simon Abrams</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I do the majority of my programming in ruby, and it’s a pleasure. The syntax is clean and concise. The standard library is full of powerful features. There’s a rich and mature community. What’s not to like?</p><p>The problems with ruby are well-documented: Performance is a concern and the concurrency model leaves something to be desired. Another problem I encountered was with ruby executables. I’ve always enjoyed writing small developer tools for myself in ruby, but since they are dependent on your ruby version, changing ruby versions can break executables or require them to be reinstalled (if you’re using something like rvm). This can become a problem when you work on an app using an old version of ruby, but your scripts (or their dependencies) use modern ruby features.</p><p>Enter Crystal! Claiming to be “Fast as C, slick as Ruby”, Crystal sounds like the performant, shareable, syntactically beautiful language I’m looking for. I decided to build a simple note-taking CLI app to introduce the language to myself. Here are some takeaways from that experience.</p><h4>Compilation forces you to handle nils, preventing runtime errors</h4><p>In my notes app, I wanted to open a file for quickly taking notes scoped to the current git branch. The idea is that when I create a feature branch, I’ll also have a notes file for that feature where I can quickly jot down relevant info. To accomplish this, I run git status and parse the results. The code started off the same in ruby and crystal (which happened a lot in this project):</p><pre>private def git_branch_name<br>  `git branch`.split(&quot;\n&quot;).find { |branch| branch =~ /^\*/ }[2..-1]<br>end</pre><p>This code just runs git branch, splits on newlines, and loops thru until it finds a line that starts with an *, which is how git marks your current branch. I expected it to work, but I got an error:</p><pre>in /Users/mgregory/.mike/apps/notes/src/notes/filename_finder.cr:23: undefined method &#39;[]&#39; for Nil (compile-time type is (String | Nil))</pre><pre>`git branch`.split(&quot;\n&quot;).find { |branch| branch =~ /^\*/ }[2..-1]<br>                                                                ^</pre><p>Crystal errors are formatted quite nicely.</p><p>The problem was that find does not guarantee a non-nil value, so calling :[]on it, could throw an error at runtime. At first, I was a little frustrated that I was being asked to handle this edge case immediately, but upon reflection, I realized that this was not an edge case, and I’d be getting runtime errors if I ran this code outside of a git init’d directory. If I were writing this in ruby, I would have realized this mistake much later and would have written a less robust application.</p><p>The solution was just to add a little nil handling:</p><pre>private def git_branch_name<br>  branch = `git branch`.split(&quot;\n&quot;).find do |branch|<br>    branch =~ /^\*/<br>  end</pre><pre>  branch[2..-1] if branch<br>end</pre><p>It’s worth noting that this method can still return nil, so further nil handling is required where it is called, but this made sense for my purposes since the public method calling it always returns a string.</p><h4>Executable crystal binaries are awesome</h4><p>In ruby, gems are the preferred way to share code. Gems are great, but they are less than ideal for sharing executable CLI apps since they are dependent on a compatible ruby version.</p><p>I’ve encountered this problems numerous times in the wild, since I regularly work on a ruby 2.1.7 application, but I regularly use tools from the ruby standard library that were introduced in later versions. In practice it looks like this:</p><pre>$ my-cli-app # works<br>$ cd old_rails_app<br>$ my-cli-app # error</pre><p>When I cd into the app, rvm (or rbenv) changes my current ruby version, a dependency of my CLI app.</p><p>With a compiled crystal binary, you don’t even need crystal to run the file. Just put the executable in a directory within your PATH environment variable and you’re good to go. Everything needed to run the app is in the binary.</p><h4>It’s fast</h4><p>It’s so much faster than ruby. I used a very simple example, but you can find much better benchmarks elsewhere. In ruby:</p><pre># testy.rb<br>arr = []<br>10_000.times do |n|<br>  arr &lt;&lt; n<br>end<br>p arr.size</pre><pre>$ time ruby testy.rb<br># 10000<br># ruby testy.rb  0.03s user 0.01s system 79% cpu 0.054 total</pre><p>In crystal:</p><pre># testy.cr<br>arr = [] of Int32<br>10_000.times do |n|<br>  arr &lt;&lt; n<br>end<br>p arr.size</pre><pre>$ time ./testy<br># 10000<br># ./testy  0.00s user 0.00s system 75% cpu 0.009 total</pre><p>Note that with crystal, I skipped the compilation step. To run a crystal file for debugging/development, you need to run:</p><pre>$ crystal run my_file.cr</pre><p>This will not be as fast as the optimized release version of your app. To build a file for release (the step I skippped):</p><pre>$ crystal build my_file.cr --release</pre><h4>The built in Crystal CLI is feature-rich</h4><p>As briefly mentioned in the previous section, crystal ships with a powerful CLI for building and running your apps. You will usually use it to build and run your apps, but there are two “hidden” features that I think every crystal developer should know about.</p><p>crystal docs</p><p>Running crystal docs from the root of a crystal app will generate full html/css/js pages for all of your classes in your app. You’ll have some TODOs to fill in, but overall this feature makes documentation much easier. The documentation pages are also searchable and includes the method signatures of your class and instance methods, what types they accept and return and the values of your constants.</p><p>crystal tool format</p><p>Who needs a linter? This tool reformats your code to fit crystal best practices. I imagine it’s very useful for maintaining code quality when working in teams. You never have to worry about that one developer who won’t look at the style guide again!</p><h4>Conclusion</h4><p>Overall, I found my experience working on this CLI in crystal to be a seamless transition coming from ruby. Crystal solved language ruby dependency problem, saved me from runtime errors, performed very well, and included some other tooling out of the box that made my life easier. I’m planning on continuing to build my other CLI apps in crystal and look forward to the language and frameworks using it to continue to grow. Crystal is a language to keep an eye on.</p><h4>More where this came from</h4><p>This story is published in <a href="http://blog.usejournal.com/">Noteworthy</a>, where thousands come every day to learn about the people &amp; ideas shaping the products we love.</p><p>Follow our publication to see more product &amp; design stories featured by the <a href="https://usejournal.com/?utm_source=usejournal.com&amp;utm_medium=blog&amp;utm_campaign=guest_post">Journal</a> team.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=28c577702083" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>