NilHash Plugin
September 13, 2006 @ 01:40 PMThis 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 |
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