Dave Thomas <Dave / PragmaticProgrammer.com> wrote:
>
>David Alan Black <dblack / candle.superlink.net> writes:
>
> > The behavior I find most surprising -- even given the underlying
> > principle of defining a variable upon seeing an assignment to it -- is
> > illustrated by this:
> >
> >    irb(main):003:0> if m then m = 123 end
> >    NameError: undefined local variable or method `m' for 
>#<Object:0x4017fc90>
> >    (irb):3:in `irb_binding'
> >    irb(main):004:0> if m then m = 123 end
> >    nil
>
>Let's play Ruby during the parsing stage (remember, we're not
>executing code at this point)
>
>    if                        <keyword, I'm looking for some
>                               kind of expression>
>       m                      <name. I haven't seen an assignment,
>                               so it's a method call>
>         then                 <keyword>
>              m = 123         <assignment to m. remember the fact that
>                               m is a variable for future reference>
>                      end
>
>
>    if                        <keyword, I'm looking for some
>                               kind of expression>
>       m                      <name. I've seen 'm' before. Let me see..
>                               yup, it's a variable, because it was
>                               assigned to>
>
>    etc...
>
>
>Now we'll play Ruby during execution:
>
>    if m then m = 123 end    < OK, I need to call method 'm' to
>                               evaluate the condition. Send 'self'
>                               the message :m. Whoops! undefined
>                               method.>
>
>    if m then m = 123 end    < OK, I need to look up the value of the
>                               variable 'm'. It's nil. The if doesn't
>                               get executed>
>
>
>For what it's worth, I agree that it's an ugly situation, but wihout
>adding declarations, or adding required parentheses to every method
>call, I'm not sure how to get around it (apart possibly from getting
>rid of local variables and making them all attribute methods of the
>current binding).
>
Cool explanation.

Let me sit in the corner and think about this one.

One of the features that I find most useful in Perl is the
strict pragma.  It doesn't stop many deep errors, but it
catches most of my typos upon trying to compile.  Now Ruby's
design is different enough that Perl's notions of strict
(which really are not very strict) are not very useful.  But
I think it would be good if during parsing Ruby kept track
of which methods get called.  If it sees a variable declared
that is the *same* as a method defined in the current class,
that may be a typo.  Likewise keep track of what you see
declared.  Have a keyword like, "method_unit" at which point
all methods used in the current class which cannot be
resolved as methods will be flagged as compile-time errors.

Those two changes would probably catch most accidental
mistakes.  Now I don't know about others.  But typos are among
my most common errors, and having them mostly caught before the
code runs speeds my development significantly.

Cheers,
Ben
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com