naught_check

Why?

Got tired of string.empty? checks in a project blowing up on nil strings.

On a Rails project where numeric fields are declared NOT NULL, you can use ActiveSupport’s .blank? and Numeric’s .zero? instead.

But .zero? isn’t nil-safe, so if you have possibly-nil numerics you would have to require ‘andand’ and remember to do number.andand.zero? for every zero-check. Or reopen Numeric.zero? in the project and add the nil-check. Or use something like this.

Usage:

require 'naught_check'
include Naught

nil.naught? => true
"".naught? => true
"    ".naught? => true
[].naught? => true
{}.naught? => true
0.naught? => true
0.00.naught? => true

.aught? does the reverse, so you don’t need the double-negative of !value.naught? (value-not-naught sounds silly)

Compared to ActiveSupport .blank?

In a Rails project, ActiveSupport .blank? would do almost the same thing, except for numbers:

0.blank? => false
0.naught? => true

It’s like ActiveSupport .blank? combined with a nil-safe Numeric .zero?

Copyright © 2010 Sean Miller. See LICENSE for details.