3 ms·
I contend that if you are using defined? you are doing something wrong. local variables should be local. If you don't know what exists in the current scope, yo
by aaronblohowiak 17y ago
I contend that if you are using defined? you are doing something wrong. local variables should be local. If you don't know what exists in the current scope, you are dealing with confusing code. Rails erb evaluation for partials makes this mistake, and creates a poor reimplementation of function calls where the function header is defined by the caller with the :locals =>{} hash.
- pvg 17y agoOr at the very least you are likely to get terribly confused. The posters confusion has absolutely nothing to do with conditionals and modifiers and is entirely about Ruby variable definition and scoping semantics. In fact if you broke one of his examples by reversing the conditional, the variable, the assignment of which is never evaluated is still defined. >> a = 1 => 1 >> unless defined? a >> b = 5 >> end => nil >> defined? b => "local-variable" >> b => nil
- angilly 17y agowhoa. didn't see that coming.
- aaronblohowiak 17y agoRight, variable definition happens as early as when the code is parsed, even if it is never used.
- jrmurad 17y agoI don't know whether they're planning to deprecate that but they do provide the alternative local_assigns hash for partials now.