Does anyone know of a way to write a function (or the name of an
existing function?) that will both check if a variable is defined, and
ensure that it is non-nil?
This has to be an uber-common problem, but hard to figure out the
right Google term for.
I have partials that use optional local variables. If an optional
variable is not passed in, and has never been passed in since my
mongrel instance started, then defined?(variable) will be false.
However, if the partial has ever been called before, the local
variable has the value of “nil” (even if it wasn’t passed in on this
call), so defined?(variable) will be true.
Thus, I am left with ugly code (defined?(variable) && variable)
littered throughout my partials.
What’s worse, I can’t seem to write a method to combine these two
checks because I get an error in my partial when I try to call a
method with a variable that isn’t defined. Bleck.