Annoying block variable behaviour !!

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I remember reading somewhere that the Ruby handling of block local=20
variables is deprecated and will be fixed, but for now it gives very anoyin=
g=20
behaviour. For example:


This is obvious and correct:

a)
=2D------------------
[1,2,3,4].each { |kk| kk=3Dtrue }
=3D> [1, 2, 3, 4]

kk
=3D> NameError: undefined local variable or method `kk' for main:Object
=2D------------------


But what about this???:

b)
=2D------------------
kk =3D false
[1,2,3,4].each { |kk| kk=3Dtrue }
=3D> [1, 2, 3, 4]

kk
=3D>true
=2D------------------


So, "kk" is defined as "false" and later in a block it's "true", but this=20
value must not be visible outside the block. In a) it doesn't occur (so OK)=
=20
but in b) is annoying, kk is finally "true" just because in a block it=20
was "true" and before it it was defined in top level.

IMHO it makes no sense at all. Is there any explanation for this last case?


Thanks for any comment about it.


=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

MjAwOC80LzE2LCBQZcOxYSwgQm90cCA8Ym90cEBkZWxtb250ZS1waGlsLmNvbT46Cgo+IHRoZSBy
dWJ5IGNvbW11bml0eSBpcyBkaXZpZGVkIHdoZW4gaXQgY29tZXMgdG8gdGhpcyBzaGFkb3dpbmcg
YmVoYXZpb3VyIG9mIGJsb2NrIGxvY2FscyBhbmQgYmxvY2sgcGFyYW1zLiBzZWUgdGhlIHJ1Ynkg
YXJjaGl2ZXMuCgpUaGFua3MsIEknbGwgbG9vayBmb3IgaXQuCgoKPiAgb24geW91ciBjYXNlLCBy
dWJ5MS45IG1heSBzYXRpc2Z5Cj4KPiAgaXJiKG1haW4pOjAwNTowKiBSVUJZX1ZFUlNJT04KPiAg
PT4gIjEuOS4wIgo+ICBpcmIobWFpbik6MDA2OjA+IFsxLDIsMyw0XS5lYWNoIHsgfGtrfCBraz10
cnVlIH0KPiAgPT4gWzEsIDIsIDMsIDRdCj4gIGlyYihtYWluKTowMDc6MD4ga2sKPgo+IE5hbWVF
cnJvcjogdW5kZWZpbmVkIGxvY2FsIHZhcmlhYmxlIG9yIG1ldGhvZCBga2snIGZvciBtYWluOk9i
amVjdAo+Cj4KPiBpcmIobWFpbik6MDA4OjA+IGtrPWZhbHNlCj4gID0+IGZhbHNlCj4gIGlyYiht
YWluKTowMDk6MD4gWzEsMiwzLDRdLmVhY2ggeyB8a2t8IGtrPXRydWUgfQo+ICA9PiBbMSwgMiwg
MywgNF0KPiAgaXJiKG1haW4pOjAxMDowPiBrawo+ICA9PiBmYWxzZQo+Cj4gIGFuZCBpdCBjb21l
cyB3IHdhcm5pbmdzIHRvbyBqdXN0IGluIGNhc2UgeW91IHdhbnQgdG8gYmUgc2FmZSA7KQoKR3Jl
YXQgdG8ga25vdyBpdCwgdGhhbmtzIGEgbG90IDopCgoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxv
CjxpYmNAYWxpYXgubmV0Pgo=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top