<?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[Able - Medium]]></title>
        <description><![CDATA[We are a product acceleration studio—a new model, built to design, develop, and deploy the right technology faster. - Medium]]></description>
        <link>https://medium.com/ableco?source=rss----47eac29aa91f---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Able - Medium</title>
            <link>https://medium.com/ableco?source=rss----47eac29aa91f---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 13 Nov 2026 08:44:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/ableco" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating a Gem Generator]]></title>
            <link>https://medium.com/ableco/creating-a-gem-generator-b42372b132ef?source=rss----47eac29aa91f---4</link>
            <guid isPermaLink="false">https://medium.com/p/b42372b132ef</guid>
            <category><![CDATA[ruby-for-beginners]]></category>
            <category><![CDATA[thor]]></category>
            <dc:creator><![CDATA[Mariella Miranda]]></dc:creator>
            <pubDate>Thu, 01 Mar 2018 04:09:03 GMT</pubDate>
            <atom:updated>2018-03-08T23:20:35.215Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eLP3IoLtFGIB6alrM2N7Lg.png" /></figure><p>Hi! In this tutorial, we’ll be creating a Gem Generator. What in the world is that? Well, it is a series of tasks that in the end will generate the scaffolding expected for a RubyGem to be functional.</p><p>As we move through this tutorial, we will be using <a href="http://whatisthor.com/">Thor</a>, a handy toolkit for building command-line interfaces.</p><p>Thor improves <a href="https://en.wikipedia.org/wiki/Rake_(software)">Rake</a>’s functionality by making it easier to add arguments and options to command line tools. The generators that ship with Rails use Thor since Rails 3.0.</p><p>Keeping this <a href="https://github.com/erikhuda/thor/wiki">wiki</a> at hand will allow you to find Thor reference materials faster.</p><p>Let’s get to it.</p><p>First, we need to make sure we have thethor gem installed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*g11ot9Pbow9gjzGjuVmA2Q.gif" /><figcaption>gem install thor</figcaption></figure><p>We need to create a folder for our Generator and then, let’s go ahead open it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*6JDkuXUIpLf6BhshLhYaeg.gif" /><figcaption>mkdir newgem — cd newgem</figcaption></figure><p>In the new folder, we’ll create the file where our Generator will be written.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*bxXh6y3Geo3yCYWaZwDLiA.gif" /><figcaption>touch newgem.thor</figcaption></figure><p>We&#39;ll be using a combination of Thor::Group which allows us to create a sequence of tasks and Thor::Actions so we can use predetermined actions on our tasks.</p><p>Next, we create a folder for the needed Gem templates, and in it, create said templates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*OnLXNxBZz5qV63MPiEWUvw.gif" /><figcaption>mkdir templates — cd templates — touch template.gemspec.tt — touch main.rb.tt</figcaption></figure><p>touch template.gemspec.tt — will be the Gemspec template <br>touch main.rb.tt — will be the Main Gem file template</p><p>Ok, so we have our files, now we’ll define what tasks we need to generate Gems on demand.</p><ol><li>Define our Generator Root File.</li><li>Create our Gem folder.</li><li>Create the gem.gemspec file in our gem&#39;s folder (using the proper template).</li><li>Create the lib folder inside our gem&#39;s folder.</li><li>Create the gem.rb file inside lib, which will be our gem&#39;s main file.</li></ol><p>Now, it is time to define those tasks in newgem.thor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d7741b2e61bdbc64d5d7b1dbc6282bcb/href">https://medium.com/media/d7741b2e61bdbc64d5d7b1dbc6282bcb/href</a></iframe><p>Let’s test this! We can see if it works by executing thor newgem hello in our terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*AHJulL1Ym7tyfM00yxqBXg.gif" /><figcaption>This should generate an empty folder with name <strong>hello</strong></figcaption></figure><p>It works! So, now let’s keep building our tasks. Our next task to be defined is the gemspec. We’ll start with the modification of the template.gemspec.tt file, to have the <a href="http://guides.rubygems.org/make-your-own-gem/#your-first-gem">information needed</a> for a Gem to be recognized as such.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/686d418952e55bbd5175077530f7f1ec/href">https://medium.com/media/686d418952e55bbd5175077530f7f1ec/href</a></iframe><p>Now that our template file has the information we need, let’s go back to newgem.thor and finish defining our tasks. We’ll start with create_gemspec:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2dc45ec3d2e516931cb5b550aaf5a2b/href">https://medium.com/media/c2dc45ec3d2e516931cb5b550aaf5a2b/href</a></iframe><p>Next, let’s move on to the create_lib_folder task</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65a190f6faa3ac2eb3d2a5b1fd68864e/href">https://medium.com/media/65a190f6faa3ac2eb3d2a5b1fd68864e/href</a></iframe><p>Then finish defining the tasks with the create_main_rb one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/378f47ab3c83dbf9e918de9c6193f9b2/href">https://medium.com/media/378f47ab3c83dbf9e918de9c6193f9b2/href</a></iframe><p>We must modify our template main.rb.tt, so it works with our Generator and add the following to our file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/900cbbbe45ff55464e04175b9bc73f70/href">https://medium.com/media/900cbbbe45ff55464e04175b9bc73f70/href</a></iframe><p>We can retest our Generator, but hold on… first, we need to erase the previous Gem we generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*qbZIVZ_qfPc9nIUJ3PKALA.gif" /><figcaption>rm -fr hello<strong> (removes old hello folder)</strong> —<strong> </strong>thor newgem hello</figcaption></figure><p>This should create our new Gem folder with the Gemspec file and the Lib folder with the Gem file. At this point, we’ve already fulfilled the initial premise for this exercise. Now, let’s spruce it up.</p><p>So, one of the things that will help us in the future with our Gems, is that they’re Git Repositories. That&#39;s why we’ll add a task to our generator. In newgem.thor add the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e786fadf5c7917aacfb8e0a9c4ce7c7/href">https://medium.com/media/8e786fadf5c7917aacfb8e0a9c4ce7c7/href</a></iframe><p>We can add an option for <a href="https://choosealicense.com/licenses/mit/">user license</a>. Again, in newgem.thor we add the following before initializing git in our folder.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af014e40ffd158236d84685a60cb626c/href">https://medium.com/media/af014e40ffd158236d84685a60cb626c/href</a></iframe><p>The cool thing is we can also make an executable command for our Gem. To do so, we’ll add this line at the start of our newgem.thor file #!/usr/bin/env ruby. Then, we’ll need to initialize our class with Newgem.start at the end of our file.</p><p>Also, we need to change the name of the file just to newgem, for that we&#39;ll carry out the following in the terminal: mv newgem.thor newgem</p><p>To initiate our Gem, first we’ll make sure the system knows it’s an executable file with chmod +x newgem in the terminal. After that, we can run our executable as many times as we want to with ./newgem nameofthegem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*mMa11LYVBPYsejzIp58iAQ.gif" /></figure><p>So, that’s it for this first post. Stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b42372b132ef" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ableco/creating-a-gem-generator-b42372b132ef">Creating a Gem Generator</a> was originally published in <a href="https://medium.com/ableco">Able</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>