Recent postings have raised the issue of local variables in Ruby and
some posters have urged that Ruby be extended so that local variables
can shadow each other, ideally with a Perl-style "my" declaration.
1. No good programmer would ever shadow their own local variables.
It is extremely dangerous, provides no benefit, and dramatically
increases programmer confusion and concomitant the risk of bugs.
2. The lisp/scheme (LET () ...) statement is _nothing_ like the perl
MY declaration. LET is used to define local variables and
establish their scope; it is the only way to create local
variables other than declaring them as formals to lambda.
No skilled lisp programmer would ever use embedded LET to shadow
their own local variables. In fact, such use of LET would be a
clear indicator that the programmer had no idea what they were
doing and their code could not be trusted. MY is a sad workaround
slapped onto a grotesque programming language after the designers
gradually came to understand that their language was an ungodly
mess, that programs written in their language were a nest of bugs,
and they really had no idea what they were doing after all.
3. Ruby has a very clean mechanism for local variables, that is, a
naming convention that clearly identifies the scope of variables,
and policy that the first use of a name declares the variable and
its scope. The Ruby mechanism is very much like a lazy man's LET
statement, which is okay in a scripting/prototyping language. Any
change to this mechanism will provide no material benefit to the
programmer, will needlessly increase the complexity of the
language, and will dramatically increase the chance that novice
programmers will inadvertently confuse and harm themselves.
On a more general point, I think that Ruby already has enough
syntactic sugar and lazy programmer's conventions. (Enough with the
repeated call for the nugatory @name initialization conventions!)
I could even argue that Ruby has gone too far in that direction [see
below]. Adding more such devices will only increase the chance that
Ruby ends up being either yet another programming language theorist's
playground or (even worse) yet another unholy hacker's mess like Perl.
Eric
[[As others have noted, Ruby's multiple assignment conventions are
baroque and provide no material benefit to the programmer. What is
the benefit of writing "a, b = x, y" when you can just as well write
"a = x; b = y"? The risk of mismatching arguments in yield and return
statements is quite high, and will lead to needless bugs in otherwise
reasonable code. The language would be both cleaner and much safer if
multiple assignment was eliminated and arity was strictly enforced.]]