Is there a "fancy" Ruby way to check whether a local variable is both defined and evaluates to true without using ands and ors?
- by Steven Xu
This is quite a quick question. I currently use
do_this if (testvar ||= false)
Which works just fine.
But this approach unnerves me because while fast, it does not short-circuit like defined?(testvar) && testvar does, and it instantiates and assigns a value to a local variable that is subsequently never used, which seems inefficient.
I enjoy the very reasonable fact that instance variables are nil before assignment, but I'd like for the situation to be just as easy for local variables.