MrProper: Cleaner blocks in Ruby
— October 28 2009
I love blocks in Ruby. I use them all the time: each, collect, map, etc. And they’re a great way of making code more readable and fun.
But I don’t like it that when I call them for a nil object, they raise an exception. So I have to add an if around them, and my codes becomes ugly. I get this often when trying to evaluate params for a form that may not have a given field, like checkboxes.
A fix for this would be using an or, so if the object is not defined the blocks will use the empty array and not get run. Like this:
But then I remembered why I love Ruby: You can rewrite everything! So I came up with #is_defined, which you can call to clean up your code in this way:
Since this is pretty cool, I turned this into a MrProper, a plugin that you can download at Github. Just drop it inside config/initializers/mrproper.rb
Note: You could also overload NilClass with methods called each, map, etc. The problem with this is that other libraries may use exception control to manage the flow in part of their code, so these solution could break things by failing silently. Therefore, it’s better to explicitly call a method to avoid errors and be aware that they won’t rise.
Update:
Thanks to @raganwald, @peteforde and @heycarsten for the interesting links. There are better alternatives to this, like andand. I enjoyed the posst about Array-ifying values and Self confident code, too.