urn:uuid:65bf7550-56d1-012b-922d-faf1622c1f62Brennan Dunn - Blog2008-08-28T01:48:55+00:00Hash#hashmap2008-07-03T14:12:56-04:002008-07-03T14:10:23-04:00<p>
One thing that always annoyed me about Ruby's enumerable methods is that they are very partial toward arrays. Sure, they play nicely with hashes or any instance that is setup to work with Enumerable, but still - hash goes in, array comes out.
</p>
<p>
I had the challenge of wanting to perform operations on the values of a given hash, and easily return a hash with just the values transformed. So I created a Hash method called hashmap.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="r">class</span> <span class="cl">Hash</span>
<span class="c"># Translate the keys/values of a hash and return a new hash</span>
<span class="c"># {:one => 'one', :two => 'two'}.hashmap { |k,v| [k.to_s.reverse, v.upcase] } # => { 'eno' => 'ONE', 'owt' => 'TWO' } </span>
<span class="r">def</span> <span class="fu">hashmap</span>(&block)
inject({}) <span class="r">do</span> |hsh, (key, value)|
arr = <span class="r">yield</span>(key, value)
hsh[arr.first] = arr.last ; hsh
<span class="r">end</span>
<span class="r">end</span>
<span class="r">end</span></pre></div>
</div>
<p>Enjoy!</p>On comparisons2008-06-28T16:33:53-04:002008-06-28T16:30:33-04:00<p>Even some really great developers far superior to me make mistakes, I discovered. I was browsing through <a href="http://www.github.com">Github</a> today, and came across the following:</p>
<div class="CodeRay">
<div class="code"><pre><span class="r">def</span> <span class="fu">is_hd?</span>
<span class="r">if</span> <span class="pc">self</span>.is_hd == <span class="i">1</span>
<span class="r">return</span> <span class="pc">true</span>
<span class="r">else</span>
<span class="r">return</span> <span class="pc">false</span>
<span class="r">end</span>
<span class="r">end</span></pre></div>
</div>
<p>This wasn't by an amateur developer, in fact, this code comes from a really great Ruby developer (name withheld). The if condition, like any condition, is asserting something. The above is simply checking if is_hd is equal to the number 1. You know that, I know that.</p>
<p>By now you've discovered a pet peeve of mine. <strong>Why is the author of this code returning true if the condition evaluates to true?</strong> How should this code <em>have</em> been written?</p>
<div class="CodeRay">
<div class="code"><pre><span class="r">def</span> <span class="fu">is_hd?</span>
is_hd == <span class="i">1</span> <span class="c"># obligatory dropping of self when not needed</span>
<span class="r">end</span></pre></div>
</div>
Local development with Passenger2008-06-25T18:46:41-04:002008-06-25T18:46:17-04:00<p>When <a href="http://metaskills.net">Ken Collins</a> first told me he was running Apache locally for development, I was confused. I thought: wow, that sounds like a lot of unnecessary stack for a local environment. And without Passenger, I'd still think the same.</p>
<p>But our friends at <a href="http://www.modrails.com/">Phusion</a> have created a really awesome module for Apache. So awesome that I'm now willing to consider Apache after having seen the light that is nginx. Passenger spawns Rails instances on demand, and allows you to be freed of spawning mongrels, and best of all, <em>no more combining multiple application route sets that have run on localhost:3000 in your browsers history!</em></p>
<p>After adding as many entries as you need into /etc/hosts, all that's left to do to free yourself of mongrel madness is to add the appropriate entries in Apache's configuration.</p>
<div class="CodeRay">
<div class="code"><pre><<span class="co">VirtualHost</span> *:<span class="i">80</span>>
<span class="co">DocumentRoot</span> <span class="s"><span class="dl">"</span><span class="k">/Users/brennandunn/dev/oss/ozark/public</span><span class="dl">"</span></span>
<span class="co">ServerName</span> dev.brennandunn.com
<<span class="rx"><span class="dl">/</span><span class="k">VirtualHost></span></span></pre></div>
</div>
<p>Now I have a handful of entries in both my hosts file and Apache's config, and am able to quickly jump around between projects. Oh yeah, and if you have a lot of ActiveResource based applications that talk back and forth to each other, definitely consider this approach. You'll never look back.</p>Taking control of form parameters2008-06-20T22:39:54-04:002008-06-20T22:12:30-04:00<p>
One often overlooked problem with creating or updating records with form data is that blank parameters (form fields left blank) are stored in the database as empty strings upon mass assignment.
</p>
<p>
I see world of difference between nil values and blank strings, and a lot of my code depends on knowing the difference. This being said, one form post can wipe out all of your nil values and replace them with strings unless you're careful.
</p>
<p>
My favorite way of getting around this has been to reject blank values when I set attributes on an ActiveRecord object.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="iv">@user</span>.attributes = params[<span class="sy">:user</span>].reject(&<span class="sy">:blank?</span>)</pre></div>
</div>
<p>
In this example, I'm getting rid of any key/value pairs in the user hash that have blank strings as values.
</p>
<p>
There are numerous ways of taking control of your form posts if you, like me, think that a nil value should mean "nothing's here, because nothing has been assigned", and strings should be reserved for substantive values. I was playing around with this snippet of code for setting default values of nil or empty strings which essentially just acts like Object#blank?.
</p>
<div class="CodeRay">
<div class="code"><pre><span class="r">class</span> <span class="cl">Object</span>
<span class="r">def</span> <span class="fu">or</span>(alt)
(respond_to?(<span class="sy">:empty?</span>) && empty?) || !<span class="pc">self</span> ? alt : <span class="pc">self</span>
<span class="r">end</span>
<span class="r">end</span>
<span class="s"><span class="dl">'</span><span class="dl">'</span></span>.or <span class="s"><span class="dl">'</span><span class="k">Default Value</span><span class="dl">'</span></span> <span class="c"># => "Default Value"</span>
<span class="s"><span class="dl">'</span><span class="k">Full</span><span class="dl">'</span></span>.or <span class="s"><span class="dl">'</span><span class="k">Do nothing</span><span class="dl">'</span></span> <span class="c"># => "Full"</span>
<span class="s"><span class="dl">'</span><span class="dl">'</span></span>.or <span class="pc">nil</span> <span class="c"># => nil</span></pre></div>
</div>
Ozark and me2008-06-20T20:01:39-04:002008-06-19T17:19:42-04:00<p>I got the crazy idea to start out writing a blogging application. Sure, I could use Mephisto or Typo, but I wanted something even simpler. Something for one blogger (me). Something that doesn't use Liquid.</p>
<p>Ozark isn't meant to be robust and feature filled, but it's not meant to be screencast worthy either. It incorporates a lot of what I've learned about rolling custom dispatchers, route sets, and renderers from having architected a CMS that powers <a href="http://www.caymanairways.com/">some</a> <a href="http://www.torontooneworld.com/">pretty</a> <a href="http://www.starwoodcaribbean.com/">big</a> <a href="http://www.westinprinceville.com/">sites</a>. If you're looking for high modularity, easy extensibility, and more, Ozark might be right up your alley.</p>
<p>Keep tabs on this blog to hear more about Ozark, the philosophy behind it, and how things are progressing. <del>Now I just need to add RSS/Atom support!</del></p>