<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Mungomash]]></title>
  <link href="http://mungomash.com/atom.xml" rel="self"/>
  <link href="http://mungomash.com/"/>
  <updated>2012-01-07T16:58:28-05:00</updated>
  <id>http://mungomash.com/</id>
  <author>
    <name><![CDATA[Chad Dalton]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Dedupe an array in Ruby]]></title>
    <link href="http://mungomash.com/blog/2012/01/07/dedupe-an-array-in-ruby/"/>
    <updated>2012-01-07T16:02:00-05:00</updated>
    <id>http://mungomash.com/blog/2012/01/07/dedupe-an-array-in-ruby</id>
    <content type="html"><![CDATA[<p>Deduping an array in Ruby using the <a href="http://ruby-doc.org/core-1.9.3/Array.html#method-i-uniq">uniq</a> method is simple.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="o">].</span><span class="n">uniq</span>   <span class="c1">#=&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
</span></code></pre></td></tr></table></div></figure>


<p>Deduping an array of objects based on a specific attibute can be more complicated.</p>

<p>Rails includes a <a href="http://api.rubyonrails.org/classes/Array.html#method-i-uniq_by">uniq_by</a> method.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="o">].</span><span class="n">uniq_by</span> <span class="p">{</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="n">i</span><span class="o">.</span><span class="n">odd?</span> <span class="p">}</span> <span class="c1"># =&gt; [1, 2]</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you are working without Rails, the Hash works nicely since it allows only one instance of each key.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="o">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="o">]</span>
</span><span class='line'><span class="no">Hash</span><span class="o">[*</span><span class="n">a</span><span class="o">.</span><span class="n">reverse</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|[</span><span class="n">i</span><span class="o">.</span><span class="n">odd?</span><span class="p">,</span><span class="n">i</span><span class="o">]</span><span class="p">}</span><span class="o">.</span><span class="n">flatten</span><span class="o">].</span><span class="n">values</span> <span class="c1"># =&gt; [1, 2]</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Add method to existing class in Ruby]]></title>
    <link href="http://mungomash.com/blog/2012/01/07/add-method-to-existing-class-in-ruby/"/>
    <updated>2012-01-07T15:16:00-05:00</updated>
    <id>http://mungomash.com/blog/2012/01/07/add-method-to-existing-class-in-ruby</id>
    <content type="html"><![CDATA[<p>Classes are not locked down in Ruby. If you wanted to add the firstName method to the String class, you could call that method on any string. For example &#8230;</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s2">&quot;Chad Dalton&quot;</span><span class="o">.</span><span class="n">firstName</span>
</span></code></pre></td></tr></table></div></figure>


<p>As with most Ruby, the code is simple and straight-forward. Here is code that allows you to write 2.weeks.ago &#8230;</p>

<div><script src='https://gist.github.com/1575865.js?file='></script>
<noscript><pre><code>require 'date'

class Weeks
  def initialize(number)
    @number = number
  end
  def ago()
    return Date.today – (@number * 7)
  end
end

class Fixnum
  def weeks
    return Weeks.new(self)
  end
end</code></pre></noscript></div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Git cheat sheet]]></title>
    <link href="http://mungomash.com/blog/2012/01/07/git-cheat-sheet/"/>
    <updated>2012-01-07T14:13:00-05:00</updated>
    <id>http://mungomash.com/blog/2012/01/07/git-cheat-sheet</id>
    <content type="html"><![CDATA[<div><script src='https://gist.github.com/1575696.js?file='></script>
<noscript><pre><code># undo a commit (file changes and index changes preserved)
git reset --soft HEAD^</code></pre></noscript></div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Powered by Octopress]]></title>
    <link href="http://mungomash.com/blog/2012/01/07/powered-by-octopress/"/>
    <updated>2012-01-07T13:11:00-05:00</updated>
    <id>http://mungomash.com/blog/2012/01/07/powered-by-octopress</id>
    <content type="html"><![CDATA[<p>This site is powered by <a href="http://octopress.org">Octopress</a>. I use <a href="http://pages.github.com/">Github Pages</a> rather than pay for hosting. Octopress provides a <a href="http://octopress.org/docs/deploying/github/">guide</a> that describes the set up necessary to deploy to github.</p>

<div><script src='https://gist.github.com/1575536.js?file='></script>
<noscript><pre><code># create a new post
rake new_post[&quot;title&quot;]

rake generate

rake preview

rake deploy</code></pre></noscript></div>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Google Cache]]></title>
    <link href="http://mungomash.com/blog/2011/12/29/google-cache/"/>
    <updated>2011-12-29T22:33:00-05:00</updated>
    <id>http://mungomash.com/blog/2011/12/29/google-cache</id>
    <content type="html"><![CDATA[<p>Ever do something really stupid while making changes to a website and wish that you could step back in time and take a look at it the way it appeared yesterday &#8230; or more importantly take a look at the HTML before you hosed it up?</p>

<p>Well, luckily Google keeps a copy of every page it crawls. To see a cached page of your web site use this url:</p>

<p>http://webcache.googleusercontent.com/search?q=cache:URL</p>

<p>For instance, the cached version of the home page of this web site could be retreived as such:</p>

<p><a href="http://webcache.googleusercontent.com/search?q=cache:http://mungomash.com">http://webcache.googleusercontent.com/search?q=cache:http://mungomash.com</a></p>

<p>But, don&#8217;t wait too long. Eventually, Google will crawl your new hosed up version :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Hello Hurricane]]></title>
    <link href="http://mungomash.com/blog/2011/12/01/hello-hurricane/"/>
    <updated>2011-12-01T11:21:00-05:00</updated>
    <id>http://mungomash.com/blog/2011/12/01/hello-hurricane</id>
    <content type="html"><![CDATA[<p>Everyone has a “Hello World” program. I am not everyone. I prefer “Hello Hurricane.”</p>

<blockquote><p>“I’ve got doors and windows<br/>
Boarded up<br/>
All your dead end fury is<br/>
Not enough<br/>
You can’t silence my love<br/>
Yeah I said hello hurricane”</p></blockquote>
]]></content>
  </entry>
  
</feed>

