Hi,
In message "[ruby-talk:12683] Re: FEATURE REQUEST: 'my' local variables"
on 01/03/15, YANAGAWA Kazuhisa <kjana / os.xaxon.ne.jp> writes:
|Then the snippet below show what value?
|
| foo = 0
| bar = 0
| 1.times do
| foo = 1
| foo := 2
| bar = foo+1
| end
|
| puts foo, bar
|
|With the `policy' rules the current implementation probably foo in the
|block is judged as block local variable at compile time, thus puts
|prints 0 and 3.
I think it would give 1 and 3.
> foo = 0
> bar = 0
> 1.times do
> foo = 1 # local variable foo
> foo := 2 # block local variable foo, WARNING!!
> bar = foo+1 # block local foo + 1
> end
> puts foo, bar # local variable foo, bar
|Introducing new variables at a beginning of the block is better for
|me. Otherwise I should be more careful of where a variable belonging
|to, since variables can suddenly become block local in a sequence of
|code.
I can understand you.
matz.