iBrasten

My methods of calculating time are far superior to yours, in every way.

 

This is the blog of Brasten Sager, a freelance software developer, Mariners fan, guitarist, haphazard philosopher.

NilHash Plugin

September 13, 2006 @ 01:40 PM

This is such a stupidly simple plugin, it’s barely worth a post. BUT

NilHash prevents chained element references from raising NoMethodErrors by provided NilClass with a limited \[\]\(key\) method.

Before:
1
2
3
4
5
6
# There are many ways to do this, here's just one.
def some_action
  return if (params[:order].nil? || 
             params[:order][:address].nil? || 
             params[:order][:address][:zip_code].nil?)
end
With NilHash:
1
2
3
def some_action
  return unless params[:order][:address][:zip_code]
end

Originally I sent this as a patch to the core team, but apparently this constitutes a “major change in behavior.” I would be interested in knowing if anyone has any problems after installing this. I don’t yet see how it’s a major behavioral change.

Installation

  # Make sure to say Y to http://svn.superruby.com/svn/plugins/
  $ script/plugin discover

  $ script/plugin install nil_hash

0 Responses to “NilHash Plugin”