<?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 Hannah Squier on Medium]]></title>
        <description><![CDATA[Stories by Hannah Squier on Medium]]></description>
        <link>https://medium.com/@hannahsquier?source=rss-fa7ee1848d5d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*G3UNWOPGhKMj2S1SsXhIrg.jpeg</url>
            <title>Stories by Hannah Squier on Medium</title>
            <link>https://medium.com/@hannahsquier?source=rss-fa7ee1848d5d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:40:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hannahsquier/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[Six Ruby Best Practices Beginners Should Know]]></title>
            <link>https://medium.com/@hannahsquier/six-ruby-best-practices-beginners-should-know-6c125280f169?source=rss-fa7ee1848d5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c125280f169</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[rails]]></category>
            <dc:creator><![CDATA[Hannah Squier]]></dc:creator>
            <pubDate>Sat, 20 Aug 2016 22:12:54 GMT</pubDate>
            <atom:updated>2016-08-20T22:12:54.695Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This article was previously</em><a href="https://www.codementor.io/ruby-on-rails/tutorial/6-ruby-best-practices-beginners-should-know"><em> published on codementor.io</em></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/0*X1fKPlobtEieQKJP." /></figure><p>The Ruby language was developed by Yukihiro Matsumoto (“Matz”) in 1995. The language was designed to be object-oriented, intuitive, and easy to learn to optimize for developer time. As “Matz” once said:</p><p>“…in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. We are the masters. They are the slaves.”</p><p>In this tutorial, we’ll explore Ruby best practices to ensure our code is in line with Matz’s principles of being legible, modular, and concise.</p><h4><strong>1) Use case/when conditionals instead of lengthy if statements.</strong></h4><p>Let’s say we are writing a program to ask for a student’s major and answer with an appropriate response. We have one variable, major, and we want to check its value and respond. We may be tempted to use an if statement with multiple elsifs. Avoid this temptation; there is a more concise way! The case/when statement allows you to check whether a variable is set to many different values and is particularly useful when responding to user input. Check it out:</p><pre>puts “What is your major?”</pre><pre>major = gets.chomp</pre><pre>case major</pre><pre>when “Biology”</pre><pre>    puts “Mmm the study of life itself!”</pre><pre>when “Computer Science”</pre><pre>    puts “I’m a computer!”</pre><pre>when “English”</pre><pre>    puts “No way! What’s your favorite book?”</pre><pre>when “Math”</pre><pre>    puts “Sweet! I’m great with numbers!” </pre><pre>else</pre><pre>    puts “That’s a cool major!”</pre><pre>end</pre><p>The case/when statement is a concise way of handling a variable with multiple possibilities.</p><h4><strong>2) Avoid for loops and use the each method with a block.</strong></h4><p>While for loops are common and accepted in most other programming languages, in Ruby we should almost universally avoid them. for loops are not as space efficient as using the each method and passing it a block because they store the variable that represents each element. See the following example:</p><pre>for elem in [1, 2, 3, 4, 5]</pre><pre>    puts elem</pre><pre>end</pre><p>Now, after the loop is complete, if we check what elem is equal to:</p><pre>puts elem #=&gt; 5</pre><p>We didn’t actually want to store the value of elem, we just wanted to use it as a name placeholder for the current element while iterating. Instead, we use the more efficient each method with a block. (A block is Ruby’s version of an anonymous function or lambda). It is much better practice to write the above code as follows:</p><pre>[1, 2, 3, 4, 5].each do |elem|</pre><pre>    puts elem</pre><pre>end</pre><p>Or even better, on one line:</p><pre>[1, 2, 3, 4, 5].each { |elem| puts elem }</pre><p>Now, if we check what elem is:</p><pre>puts elem #=&gt; NameError: undefined local variable elem or method ‘elem’ for main:Object</pre><p>Ahhh, that’s better!</p><h4><strong>3)</strong> <strong>Use the splat (*) operator for methods with a variable number of inputs.</strong></h4><p>The main thing the splat operator is used for is grouping together a collection of inputs into an array. Before we dive into specifics of why it’s used, let’s see it in action.</p><pre>def mood(name, *feelings)</pre><pre>    feelings.each do |feeling|</pre><pre>        puts “#{name} is feeling #{feeling} today.”</pre><pre>    end</pre><pre>end</pre><pre>mood(“Suzie”, “happy”, “excited”, “nervous”)</pre><pre># Suzie is feeling happy today.</pre><pre># Suzie is feeling excited today.</pre><pre># Suzie is feeling nervous today.</pre><p>People can feel many different things or they can just feel one thing. Because we have a variable number of inputs, we use the splat operator, which groups all of the inputs after the defined inputs into an array. The splat operator also has some other fancy use cases. Let’s take a look at one of them.</p><pre>pablo_picasso_full_name = “Pablo Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y Picasso”</pre><pre>first, *other_names, last = pablo_picasso_full_name.split</pre><pre>first.join(“ “) #=&gt; “Pablo”</pre><pre>other_names.join(“ “) #=&gt; “Diego José Francisco de Paula Juan Nepomuceno María de los Remedios Cipriano de la Santísima Trinidad Ruiz y”</pre><pre>last.join(“ “) #=&gt; “Picasso”</pre><p>(Yes, that’s Pablo Picasso’s full name!) Whenever we see a splat, we can just think of it as a way wrangling a collection of an unknown number of items into an organized array so we can work with it more easily.</p><h4><strong>4) “Monkey patch” pre-defined methods so they work better for our specific needs.</strong></h4><p>Let’s imagine we are designing a chess game. Our board is represented as an array of arrays, where each inner array is a row on the board. If we wanted to access a piece, we would get it like so: board[row][column]. We are going to be accessing pieces many times, and it would be easier if we could just call it like this instead: board[row, column]. So let’s take matters into our own hands and redefine the [] method!</p><pre>class Board &lt; Array</pre><pre>    def [](row, column)</pre><pre>        self[row][column]</pre><pre>    end</pre><pre>end</pre><p>We were able to do this because [] is really just a method on the Array class and our Board class inherits from Array. It is also easier to see that [] is just a method if we rewrite array[i] as array.[](i). Now, we can read what any piece is by just calling board[row, column]. But what if we also want to be able to move a piece by changing a value of board like this: board[row, column] = new_piece. To do this, we have to overwrite the []= method. See below.</p><pre>class Board &lt; Array</pre><pre>    …</pre><pre>    def []=(row, column, new_piece)</pre><pre>        self[row][column] = new_piece</pre><pre>    end</pre><pre>end</pre><p>Ruby is ultra object-oriented, which makes it quite easy to rewrite predefined methods so that they work better for us. Remember Matz’s ultimate goal? Keep the developer happy!</p><h4><strong>5) Use double bang to determine if a value exists.</strong></h4><p>The bang symbol (!) turns any truthy value into false and any falsey value to true. Suppose we were trying to determine if a person has a middle name.</p><pre>class Name</pre><pre>    def initialize(first_name, last_name, middle_name=nil)</pre><pre>        @first_name = first_name</pre><pre>        @middle_name = middle_name</pre><pre>        @last_name = last_name</pre><pre>    end</pre><pre>    def has_middle_name?</pre><pre>        !!@middle_name</pre><pre>    end</pre><pre>end</pre><pre>Name.new(“George”, “Washington”).has_middle_name? #=&gt; false</pre><pre>Name.new(“Barack”, “Obama”, “Hussein”).has_middle_name? #=&gt; true</pre><p>This one is a bit tricky, so let’s walk through it step-by-step for the above examples. For the George Washington example, @middle_name = nil. Nil is not equal to false, but it is a falsey value. So, if we had just returned @middle_name without the !!, we would have gotten nil, not false like we wanted. Therefore, we use one bang to turn nil into true and then another bang to turn true into false. See below:</p><pre>@middle_name #=&gt; nil</pre><pre>!@middle_name #=&gt; true</pre><pre>!!@middle_name #=&gt; false</pre><p>Likewise, Barack Obama’s middle name is “Hussein”, which is a truthy value. In the method has_middle_name?, we don’t want to return @middle_name, we just want to return true if @middle_name is truthy. See below for a walk through of how has_middle_name? works for the Barack Obama example.</p><pre>@middle_name #=&gt; “Hussein”</pre><pre>!@middle_name #=&gt; false</pre><pre>!!@middle_name #=&gt; true</pre><p>If we hadn’t used a double bang, we would have had to use a wordy if statement to check if @middle_name is nil or not. The double bang is an elegant way of creating the common “does some variable exist?” method.</p><h4><strong>6) Use symbols instead of strings in hashes.</strong></h4><p>Symbols are a Ruby specific data type denoted like so, :symbol. Symbols are similar to strings, except they are immutable and are used to name variables. They should be used whenever you are storing the name of something that does not have to be mutated. They are more efficient than strings because they are stored in one spot in memory.</p><pre>a = :apple</pre><pre>b = :apple</pre><pre>a.object_id == b.object_id #=&gt; true</pre><pre>a = “apple”</pre><pre>b = “apple”</pre><pre>a.object_id == b.object_id #=&gt; false</pre><p>As we can see in the example above, when we set two variables equal to the same symbol, they point to the same address in memory. However, when we perform the same operation with a string, this is not the case. Thus, we should use symbols for improved space efficiency when we do not need to mutate its value. Now, let’s examine how we can use symbols as hash keys instead of strings. Let’s say we are trying to count how many of each plant we have in our garden and are keeping track in a hash. Instead of using strings like so,</p><pre>our_garden = { “roses” =&gt; 9, “rhododendrons” =&gt; 2, “poppies” =&gt; 12, “geraniums” =&gt; 6 , “sneezeworts” =&gt; 5 }</pre><p>we should use symbols:</p><pre>our_garden = { :roses =&gt; 9, :rhododendrons =&gt; 2, :poppies =&gt; 12, :geraniums =&gt; 6, :sneezeworts =&gt; 5 }</pre><p>Or even better, Ruby hashes that have symbols as keys can be written more concisely like so.</p><pre>our_garden = { roses: 9, rhododendrons: 2, poppies: 12, geraniums: 6, sneezeworts: 5 }</pre><p>And when we’re trying to count rhododendrons and sneezeworts, it’s crucial that we are concise!</p><p>By keeping our code succinct and object-oriented we can write programs in line with Matz’s philosophy that the development process should first and foremost be optimized for the developer, not the computer. We are more efficient and happier if we are not bogged down with syntax and mundane specifics. When we are happier, we write better programs! Thank you Matz for keeping us happy! :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c125280f169" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get Another Number Algorithm]]></title>
            <link>https://medium.com/@hannahsquier/get-another-number-algorithm-9a6d24367a9f?source=rss-fa7ee1848d5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9a6d24367a9f</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[women-in-tech]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[logic]]></category>
            <category><![CDATA[algorithms]]></category>
            <dc:creator><![CDATA[Hannah Squier]]></dc:creator>
            <pubDate>Sun, 14 Aug 2016 22:21:39 GMT</pubDate>
            <atom:updated>2016-08-14T22:21:39.947Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, we will explore the naive and optimized solutions for a simple interview-type problem.</p><h3><strong>The Problem</strong></h3><p>Given an array (arr) of n unique non-negative integers, return a non-negative integer that is <strong>not</strong> in the array. Return nil if the array contains all possible integers.</p><h3>Unoptimized Solution</h3><p>In this solution we search the array for integer n, and return n if it is not in the array. We iterate through every integer from zero to the maximum integer in array + 1 to capture all possible integers that might not be in the array. See below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/470c50baa631225f85306d5c4d12cf10/href">https://medium.com/media/470c50baa631225f85306d5c4d12cf10/href</a></iframe><p>In this solution we search through the entire array every time we want to check if the number, n, is in it. For this reason, it is a very unoptimized solution.</p><p><em>Time Complexity: </em>O(m * n) where n is the length of arr and m is the max value in arr + 1.</p><p><em>Space Complexity: O(1)</em></p><h3>Optimized Solution</h3><p>We can improve upon the above solution by using a hash, which has constant time look up. We will be sacrificing some space complexity because we will be using another data structure, but we will dramatically improve time complexity. See below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6eb4eb222dfbac4d21d1b327a5b54538/href">https://medium.com/media/6eb4eb222dfbac4d21d1b327a5b54538/href</a></iframe><p>We store all the elements of the array in a hash and then look up that number in the hash. Because looking up in a hash is constant time, this is a much more optimized solution.</p><p><em>Time Complexity: </em>O(n + m) where n is the length of array, arr, and m is the smallest positive integer that is not in arr.</p><p><em>Space Complexity: </em>O(n) where n is the length of array, arr.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9a6d24367a9f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Two Algorithms for Solving Vignere’s Cipher in Ruby]]></title>
            <link>https://medium.com/@hannahsquier/two-algorithms-for-solving-vigneres-cipher-in-ruby-77bf0b37507a?source=rss-fa7ee1848d5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/77bf0b37507a</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[cipher]]></category>
            <category><![CDATA[interview-questions]]></category>
            <dc:creator><![CDATA[Hannah Squier]]></dc:creator>
            <pubDate>Mon, 18 Jul 2016 01:22:37 GMT</pubDate>
            <atom:updated>2016-07-18T01:22:37.217Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*1qhF5vOnJLz5-Zf3sBoPYg.jpeg" /></figure><p>In this tutorial, we’ll cover two different algorithms to encrypt text with <a href="https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher">Vignere’s Cipher</a>. One is a more naive but concise solution and the second is more computationally efficient. We’ll discuss the time and space complexity of each and conclude with how to design your code so it is as efficient as it can be.</p><h4>Background</h4><p>Vignere’s Cipher is a variation on the more well known Caesar’s cipher. Before diving into Vignere its important to understand how Caesar’s cipher works.</p><p>Caesar’s cipher takes a single integer key, and offsets each character in the target string by that many letters in the alphabet. So with a key of 2, the following letters would map like so: a → c, r → t, z → b, etc. Encrypting “iloveruby” with a key of 2, yields “knqxgtwda.” Every letter is shifted in the alphabet by the key and letters wrap around the alphabet.</p><p>Instead of one key, Vignere takes in a key sequence as an array ([1, 2, 3]). Every letter is shifted by the corresponding key in the key sequence. The key sequence repeats itself once the end of the pattern is reached. See below how Vignere’s Cipher encrypts “iloveruby” with key sequence [1, 2, 3]</p><pre>Word &quot;iloveruby&quot;<br>Keys: [1, 2, 3]</pre><pre>Word:   i l o v e r u b y<br>Keys:   1 2 3 1 2 3 1 2 3 <br>Cipher: j n r w g u v d b</pre><h4>The Problem</h4><p>Write a method that takes a string and a key-sequence and returns the encrypted word using Vignere’s Cipher. For now, assume all characters are lowercase a-z characters.</p><h4>Method #1: Concise But Inefficient</h4><p>In this method we begin by initializing an empty string that will be the encrypted version of our word. We also initialize a constant ALPHABET which is an array of strings from ‘a’ to ‘z.’ Then, we iterate through each character in the word being encrypted. We simultaneously index through the keys by using the modulus. On each iteration, we create a shifted alphabet by adjusting ALPHABET by the key amount. We then map the current character to the correct character in the shifted alphabet and append it to the encrypted string. See the pseudocode and solution below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7a335b3fa857a856b0475a00f96316e/href">https://medium.com/media/c7a335b3fa857a856b0475a00f96316e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2186b03563ed0545bef132349e4278a6/href">https://medium.com/media/2186b03563ed0545bef132349e4278a6/href</a></iframe><p>This method is concise, but has a few drawbacks. First of all, the shifted alphabet is re-created in memory on each iteration. If the encrypted word were 20 letters long, the shifted_alphabets would be re-created 20 times. With this approach we don’t have to handle the case where the mapped letter wraps around the alphabet (z → a).</p><p>The second drawback of this method is related to the way we store the ALPHABET constant. ALPHABET is an array, so every time we search through it for the current character to find the corresponding index, it is an O(n) operation. As you will see in the next method, if we had used a hash instead of an array, this would only be an O(1) operation.</p><p><em>Space Complexity: O(m), where m is the length of the alphabet. </em>The only additional thing being created in memory is the shifted_alphabet which is m elements long.</p><p><em>Time Complexity: O(m*n), where m is the length of the alphabet and n is the length of the word. </em>Indexing through the array ALPHABET (line 8) is an O(m) operation. We do this n times because we have to do it on each iteration. Therefore, the time complexity is the product of the length of word and the length of ALPHABET.</p><p><em>Conciseness &amp; Readability: 8/10</em></p><h4>Method #2: Trickier But More Efficient</h4><p>Instead of creating a shifted alphabet, in this method, we use indexing and hashes. One hash maps each letter of the alphabet to an index and the other maps an index to each letter. The indexing makes mapping each letter to its encrypted version a bit trickier because we have to handle the wrap around case, but it saves memory. Follow along with the pseudocode and real code below to see how its done.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03990e2d6da818561db5e4182710997b/href">https://medium.com/media/03990e2d6da818561db5e4182710997b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8be803e8b593ee053c18ad28926a08b/href">https://medium.com/media/f8be803e8b593ee053c18ad28926a08b/href</a></iframe><p>Instead of recreating a shifted alphabet, this method maps each character to its shifted character by using indexing and hashes. While rewriting the alphabet has a space complexity of O(m), we do not need any extra memory when we use indexing into a hash.</p><p><em>Space Complexity: O(1) </em>There are no new variables being stored in memory in this method, so it has constant space complexity.</p><p><em>Time Complexity: O(n). Where n is the length of the string. </em>We iterate through each character in word, so this method has an O(n) time complexity. We do not have to include the complexity of indexing through the alphabet because we used two hashes instead of an array to represent the alphabet. Finding a value in a hash by its key is only an O(1) operation.</p><p><em>Conciseness &amp; Readability: 6/10</em></p><h4>In Conclusion</h4><p>Method 1 is a more concise but less efficient solution. This solution could be the best option if readability is most important. However, Method 2 has a better time and space complexity, so as the length of the word grows, this becomes the better solution.</p><p>Both of these methods could be augmented to handle cases where not all the characters are lowercase alpha characters. This makes the problem a bit more challenging, but does not change the general logic of the problem. Also, this cipher could be adjusted to handle different alphabets. (Fun fact: the Khmer alphabet has 74 characters! How would this affect Vignere Cipher efficiency?)</p><p>There are many different ways to solve every algorithmic challenge, but each way has its tradeoffs. The important part is knowing what is best for your specific use case and how design decisions affect efficiency.</p><p>Icqrz Epfjph*! :)</p><p>*Decode the secret message with Vignere’s Cipher and a [1,2] key sequence.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77bf0b37507a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Step-by-Step Guide to Creating Your Own Rock, Paper, Scissors Ruby Gem]]></title>
            <link>https://medium.com/@hannahsquier/a-step-by-step-guide-to-creating-your-own-rock-paper-scissors-ruby-gem-4b57ff540ead?source=rss-fa7ee1848d5d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b57ff540ead</guid>
            <category><![CDATA[gem]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[tutorial]]></category>
            <dc:creator><![CDATA[Hannah Squier]]></dc:creator>
            <pubDate>Mon, 11 Jul 2016 00:30:45 GMT</pubDate>
            <atom:updated>2016-07-18T01:26:02.656Z</atom:updated>
            <content:encoded><![CDATA[<p>This tutorial will take you through the steps to make your own Rock, Paper, Scissors command line game. Before you begin, you should be fairly comfortable with object oriented programming in Ruby. By the end you will have your own Ruby Gem that can be downloaded by anyone anywhere. Make a gem and get your code out there!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/250/1*IEGD8v1AX0w2J6X3QaQT8A.png" /></figure><h4>Initialize your Gem</h4><p><strong>What is a gem?</strong> A gem is just a library of Ruby code hosted on rubygems.org that can be downloaded and used by anyone. Let’s begin!</p><ol><li><strong>Create an account</strong> on <a href="http://rubygems.org/sign_up">rubygems.org</a>.</li><li><strong>Name your gem</strong>. Choose an original name. Feel free to search all gems and find one that is not taken and speaks to you.</li><li><strong>Install bundler. </strong>Open up your command line and install the gem bundler. Bundler is a gem that helps you manage gems. It allows you to have the correct versions of all the other gems you need to run your gem and initializes your gem for you. [Note: You may want to use RVM to setup a separate Ruby installation. Here is a helpful <a href="http://stackoverflow.com/questions/14607193/installing-gem-or-updating-rubygems-fails-with-permissions-error">Stack Overflow post</a>.]</li></ol><pre>$ gem install bundler</pre><p>4. <strong>Initialize Gem. </strong>Initialize your gem with the bundle gem command. I chose to name my gem ‘rps10k,’ so wherever you see ‘rps10k’ substitute the name you chose for your Rock, Paper, Scissors gem. The following command will set up your file tree according to gem specifications.</p><pre>$ bundle gem rps10k<br>$ cd rps10k</pre><p>Your file tree should now look like this:</p><pre>.</pre><pre>├── CODE_OF_CONDUCT.md</pre><pre>├── Gemfile</pre><pre>├── LICENSE.txt</pre><pre>├── README.md</pre><pre>├── Rakefile</pre><pre>├── bin</pre><pre>│   ├── console</pre><pre>│   └── setup</pre><pre>├── lib</pre><pre>│   ├── rps10k</pre><pre>│   │   └── version.rb</pre><pre>│   └── rps10k.rb</pre><pre>├── rps10k.gemspec</pre><pre>└── spec</pre><pre>├── rps10k_spec.rb</pre><pre>└── spec_helper.rb</pre><p>5. <strong>Initialize gem as GitHub repository. </strong>We will use GitHub as a link between your local gem and rubygems.org, where your gem will be hosted. (If you do not have git and GitHub set up on your computer, follow <a href="http://burnedpixel.com/blog/setting-up-git-and-github-on-your-mac/">these instructions</a>.) First, create a new repository on <a href="http://github.com/">github.com</a> with the name of your gem. Do not check “Initialize this repository with a README.” Now, run the following commands to initialize your repo. When you add your remote branch called origin, make sure you put in the link to your repository, not mine! (Make sure you are in your gem directory!)</p><pre>$ git init<br>$ git add -A<br>$ git commit -m &quot;Initialize gem&quot;<br>$ git remote add origin <a href="https://github.com/hannahsquier/rps10k.git">https://github.com/hannahsquier/rps10k.git</a> <br>$ git push origin master</pre><p>6. <strong>Edit .gemspec file. </strong>Open up your rps10k.gemspec file. This file contains all the specifications your gem needs to run. In order for your gem to work, you will have to fill in all the TODO statements. Make the following edits to your file, but of course, fill in your personal information.</p><p>Change</p><pre>spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}</pre><p>to</p><pre>spec.summary = %q{Rock, Paper, Scissors}</pre><p>Change</p><pre>spec.description = %q{TODO: Write a longer description or delete this line.}</pre><p>to</p><pre>spec.description = %q{A simple rock, paper, scissors command line game.}</pre><p>Change</p><pre>spec.homepage = &quot;TODO: Put your gem&#39;s website or public repo URL here.&quot;</pre><p>to (any valid website will do)</p><pre>spec.homepage = &quot;<a href="https://www.hannahsquier.com/">https://www.hannahsquier.com</a>&quot;</pre><p>And, lastly, change</p><pre>spec.metadata[&#39;allowed_push_host&#39;] = &quot;TODO: Set to &#39;<a href="http://mygemserver.com&/#39;">http://mygemserver.com&#39;</a>&quot;</pre><p>to</p><pre>spec.metadata[&#39;allowed_push_host&#39;] = &#39;<a href="http://mygemserver.com&/#39;">https://www.rubygems.org&#39;</a></pre><p>Now you’re ready to start coding!</p><h4>Add Scaffold Code to your Gem</h4><ol><li><strong>High level overview of how game will work. </strong>The Game class will be initialized with two players. The Game class will have a play loop that gets each players moves until someone wins. One Player will be a Human and one will be a Computer. Both Human and Computer classes will be subclasses of Player. The Human and Computer objects will have the same public methods and attributes. The only difference between Human and Computer is that Human will get the move as standard input from the command line and Computer will chose between rock, paper and scissors at random. Here is some psuedocode that outlines the game logic.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08fb701c7f95b4e68a7c136d12019e7d/href">https://medium.com/media/08fb701c7f95b4e68a7c136d12019e7d/href</a></iframe><p>2. To begin, create the files for each class you will need in the lib/rps10k directory. You should be in your gem folder, that has the name of your gem. Enter the following in the command line.</p><pre>$ touch lib/rps10k/game.rb<br>$ touch lib/rps10k/player.rb<br>$ touch lib/rps10k/human.rb<br>$ touch lib/rps10k/computer.rb</pre><p>3. Next, open game.rb and begin turning pseudocode into real code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de5fb21e64a970fd4c2f743be4ba191e/href">https://medium.com/media/de5fb21e64a970fd4c2f743be4ba191e/href</a></iframe><p>4. Now that the game logic is laid out, fill in the Player class. Each player will have a move attribute that must be readable by the game class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3412d4c923c422d06713880fae877fc2/href">https://medium.com/media/3412d4c923c422d06713880fae877fc2/href</a></iframe><p>5. Setup the Human class which is a subclass of Player.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6851d2cd372cf2e0a53916882820a20/href">https://medium.com/media/f6851d2cd372cf2e0a53916882820a20/href</a></iframe><p>6. Next, set up the scaffolding for the Computer class, which is a subclass of Player.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cec878dab2ffcbef98ed91a0c397c4a/href">https://medium.com/media/0cec878dab2ffcbef98ed91a0c397c4a/href</a></iframe><p>7. Lastly, add a class method to the Rps10k module and require all the classes you just made so the game can be initiated. The module file (rps10k.rb) is under the lib directory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8de907058dcd39e0a3648cd045f56eec/href">https://medium.com/media/8de907058dcd39e0a3648cd045f56eec/href</a></iframe><p>Great! You have created the basic scaffolding for all the classes and modules you will need. Now just fill in the scaffolding with real code. To see my final gem, check out my <a href="https://github.com/hannahsquier/rps10k">GitHub Repo</a>.</p><p><strong>Things to note:</strong></p><ul><li>Save all your classes under lib/rps10k.</li><li>All classes should be within Rps10k module.</li><li>Don’t forget to use require_relative to import your other class files.</li><li>Commit your changes to GitHub every time you finish a logically contained portion of the gem.</li></ul><h4>Release your Gem to the Public</h4><ol><li>Add an executable file to your gem, by adding a folder called exe to your gem and filling that folder with an exe file (rps10k). Your executable file should look like this:</li></ol><p>2. Now you’re ready to push all your changes to your GitHub repo.</p><pre>$ git add -A<br>$ git commit -m &quot;Finish game logic&quot;<br>$ git push --set-upstream origin master</pre><p>3. Finally, you can release your gem to rubygems.org</p><pre>$ rake release</pre><p>You may be prompted to enter your rubygems.org credentials. Now anyone can download your gem using the following command. (With your gem name of course instead of rps10k).</p><pre>$ gem install rps10k</pre><p>Congratulations! You have created open source code and contributed to the developer community. If you have any questions, leave a comment. Stay tuned for more tutorials :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b57ff540ead" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>