When we are enjoying the freedom of using variable without
declearation, we are also loosing control over scope.


Mohammad



On 3/22/06, David King Landrith <dlandrith / mac.com> wrote:
> On 3/22/06, Mark Volkmann <mkhan / lextranet.com> wrote
                       ^^^^^^^^^^^^^^^^^^^^^^^^^
                       gmail bug !!! ..

:
>
> > It's true that blocks introduce a new scope. However, if they use
> > variables that are in scope outside the block, they don't shadow
> > those variables, they use them.
>
> about this block posted by Mohammad Khan
>
> > batman = 'robin'
> > [ 'cat', 'dog', 'horse', 'chicken' ].each { |batman| puts batman }
> > puts "Now you might think this would say 'robin', but it says: #
> > {batman}"
>
> where the last line outputs:
>
> > Now you might think this would say 'robin', but it says: chicken
>
> With no variable declarations in Ruby, there's no way to control the
> scope, so the "first occurrence dictates scope" rule applies to
> Mohammad's example. I think that this is counter intuitive. I think
> that the pipe operators should be treated analogously to the way that
> C++ treats declarations that occur within an expression; viz., they
> are scoped to the end of the block dependent upon the expression.
>
> Specifically, in C++ you have this:
>
> int i = 5;
> for (int i = 0; i < 100; i++) ...
> printf("i=%u\n", i);
>
> vs
>
> int i = 5;
> for (i = 0; i < 100; i++) ...
> printf("i=%u\n", i);
>
> I contend that the pipe operators are most intuitively understood as
> being an example of the first type of operation and not the second.
> It's safer, in any case, not to use duplicate variable names. Perhaps
> I'm too comfortable with this sort of thing because of the deeply
> entrenched habits accumulated from a background primarily in C, C++,
> and (later) Java. In perl, this would require the use of a local
> scope, which I've always hated.
>
> In any case, before I ran the script that Mohammad has posted here, I
> fully expected such a piece of code to behave like the first instance
> and not like the second. This would have caused a bug that would have
> been nearly impossible for me to find.
>
> -------------------------------------------------------
>    David King Landrith
>     (w) 617.227.4469x213
>     (h) 617.696.7133
>
>    One useless man is a disgrace, two
>    are called a law firm, and three or more
>    become a congress   -- John Adams
> -------------------------------------------------------
> public key available upon request
>
>
>
>
>
>