Is there a "fancy" Ruby way to check whether a local variable is both defined and evaluates to true without using ands and ors?
Posted
by
Steven Xu
on Stack Overflow
See other posts from Stack Overflow
or by Steven Xu
Published on 2011-01-07T05:24:53Z
Indexed on
2011/01/07
6:53 UTC
Read the original article
Hit count: 227
ruby
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.
© Stack Overflow or respective owner