[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parameter in a block is not local?

SpringFlowers AutumnMoon

10/14/2007 2:43:00 PM

I thought a iterator with a block is like an nameless function call...
so if it is a function call, the parameter is local.
but for the following, the output is surprising:

a = 1
p a

1.upto(10) {|a| p a}

p a

--------------

E:\>ruby test_iterator.rb
1
1
2
3
4
5
6
7
8
9
10
10

a is changed!

but is it true that the "a" is not global to begin with....
but then, isn't the "a" inside the block more local than the "a"
outside?

like in Pascal, i think there can be nested functions and therefore,
there will be local and then "local that is more local" than the outer
local.
--
Posted via http://www.ruby-....

20 Answers

7stud --

10/14/2007 4:23:00 PM

0

SpringFlowers AutumnMoon wrote:
> I thought a iterator with a block is like an nameless function call...
> so if it is a function call, the parameter is local.
> but for the following, the output is surprising:

Reread pickaxe2, p.51, second paragraph.
--
Posted via http://www.ruby-....

7stud --

10/14/2007 4:27:00 PM

0

SpringFlowers AutumnMoon wrote:
> I thought a iterator with a block is like an nameless function call...
> so if it is a function call, the parameter is local.
> but for the following, the output is surprising:
>

Also, I think I recall reading a post by Matz somewhere where he
shouldered the blame for that "feature" and said it was a design
mistake.
--
Posted via http://www.ruby-....

David A. Black

10/14/2007 4:29:00 PM

0

ara.t.howard

10/14/2007 4:43:00 PM

0


On Oct 14, 2007, at 8:42 AM, SpringFlowers AutumnMoon wrote:

> I thought a iterator with a block is like an nameless function call...
> so if it is a function call, the parameter is local.
> but for the following, the output is surprising:
>
> a = 1
> p a
>
> 1.upto(10) {|a| p a}
>
> p a

one way to think about it (not entirely correct) is this

- if the parameter exists *already* it is reused
- if not a block local var is created

this

1.upto(10){|a| p a}

p a

will throw an error - as you probably know. it's a fine line between
having function call semantics and closure ones. we all want this to
work

message = 'hello world'
42.times{ p message }

of course. so it's rather tricky. you can search the archives but i
*think* the new rule is that introducing a var like so

method{ |some_var| ...

will introduce a block local var (as you are expecting above), while
this

method{ p some_var

will not. i could be confused on this though, it really gets
confusing to not violate POLS (referring to mine here) in all cases

http://eigenclass.org/hiki.rb?Changes+in+R...
http://www.davidflanagan.com/blog/2007_08.h...

and, of course, search the archives - matz has spelled out the
changes several times.

kind regards.

a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




David A. Black

10/14/2007 4:51:00 PM

0

Pat Maddox

10/14/2007 5:01:00 PM

0

On 10/14/07, SpringFlowers AutumnMoon <summercoolness@gmail.com> wrote:
> I thought a iterator with a block is like an nameless function call...
> so if it is a function call, the parameter is local.
> but for the following, the output is surprising:
>
> a = 1
> p a
>
> 1.upto(10) {|a| p a}
>
> p a
>
> --------------
>
> E:\>ruby test_iterator.rb
> 1
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 10
>
> a is changed!
>
> but is it true that the "a" is not global to begin with....
> but then, isn't the "a" inside the block more local than the "a"
> outside?
>
> like in Pascal, i think there can be nested functions and therefore,
> there will be local and then "local that is more local" than the outer
> local.
> --
> Posted via http://www.ruby-....
>
>


This seems to me along the same lines as

a = 1
if true
a = 10
end
p a # => 10

Which certainly is not surprising or unwelcome.

Pat

Wolfgang Nádasi-donner

10/14/2007 5:12:00 PM

0

SpringFlowers AutumnMoon wrote:
> a = 1
> p a
>
> 1.upto(10) {|a| p a}
>
> p a
>
> --------------
>
> E:\>ruby test_iterator.rb
> 1
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 10
>
> a is changed!

!Remark only!

There will be a change for soon upcoming Ruby 1.9.1:

irb(main):001:0> a = 1
=> 1
irb(main):002:0> p a
1
=> nil
irb(main):003:0> 1.upto(10) {|a| p a}
1
2
3
4
5
6
7
8
9
10
=> 1
irb(main):004:0> p a
1
=> nil

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

Rick DeNatale

10/14/2007 6:28:00 PM

0

On 10/14/07, SpringFlowers AutumnMoon <summercoolness@gmail.com> wrote:
> I thought a iterator with a block is like an nameless function call...

Similar, but not the same. Blocks are closures, so for example:

a = 1
1.times {p a}

will print 1 since a in the block refers to a in the outer scope.


> so if it is a function call, the parameter is local.
> but for the following, the output is surprising:
>
> a = 1
> p a
>
> 1.upto(10) {|a| p a}
>
> p a
>
> --------------
>
> E:\>ruby test_iterator.rb
> 1
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 10
>
> a is changed!
>
> but is it true that the "a" is not global to begin with....
> but then, isn't the "a" inside the block more local than the "a"
> outside?

In Ruby 1.8 a block argument acts like a local in the containing scope.

This is changing in 1.9

http://eigenclass.org/hiki/Changes+in+R...


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

SpringFlowers AutumnMoon

10/14/2007 11:20:00 PM

0

Pat Maddox wrote:
> This seems to me along the same lines as
>
> a = 1
> if true
> a = 10
> end
> p a # => 10
>
> Which certainly is not surprising or unwelcome.

when compared to the following it is surprising:

a = 1
p a

def foo(a)
p a
end

for i in 1..10
foo(i)
end

p a

------------------

E:\>ruby test_iterator2.rb
1
1
2
3
4
5
6
7
8
9
10
1

--
Posted via http://www.ruby-....

SpringFlowers AutumnMoon

10/14/2007 11:50:00 PM

0

Rick Denatale wrote:
> This is changing in 1.9
>
> http://eigenclass.org/hiki/Changes+in+R...

I don't know what software install Ruby 1.9 on my computer

C:\Users\Mike\ruby-1.9.0-20060415-i386-mswin32

and then if I run ruby 1.9, it gives me the same result:

C:\Users\Mike\ruby-1.9.0-20060415-i386-mswin32\bin>ruby test_iterator.rb
1
1
2
3
4
5
6
7
8
9
10
10
"=========="
"1.9.0"

---------------------------
Code


a = 1
p a

1.upto(10) {|a| p a}

p a

p "=" * 10

p RUBY_VERSION

--
Posted via http://www.ruby-....