# Parameter in a block is not local?

**URL:** <https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194>\
**Category:** Ruby\
**Created:** [October 14, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194 "2007-10-14T16:42:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/1 "2007-10-14T16:42:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/2 "2007-10-14T18:22:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 6:26pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/3 "2007-10-14T18:26:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/4 "2007-10-14T18:31:14Z")

</div>

Hi –

On Sun, 14 Oct 2007, SpringFlowers AutumnMoon wrote:

> 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?

a is local, not global. The semantics of block parameters is  
assignment semantics:

|a|

basically means “a = …”, in the same scope as the scope in which the  
block is created. Blocks are different in that respect from methods,  
and the || notation (instead of (), as in methods) is a good reminder  
that they’re not the same. (Not that || specifically implies  
assignment semantics, but it’s a reminder that there’s a difference.)

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 6:44pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/5 "2007-10-14T18:44:07Z")

</div>

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

[eigenclass.org](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l7)  
[http://www.davidflanagan.com/blog/2007\_08.html#000137](http://www.davidflanagan.com/blog/2007_08.html#000137)

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

kind regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/6 "2007-10-14T18:51:07Z")

</div>

On Mon, 15 Oct 2007, David A. Black wrote:

> a is local, not global. The semantics of block parameters is  
> assignment semantics:
> 
> |a|
> 
> basically means “a = …”, in the same scope as the scope in which the  
> block is created.

I should amend that by noting that it’s a “one-way valve”: if the  
variable exists before the block is created, the scope is flat,  
whereas if the variable doesn’t exist yet, it also doesn’t exist when  
the block exits.

In other words, the local scope enters the block, from the left, so to  
speak, bringing all its bindings with it; and it exits from the right  
with only those bindings, which may or may not have been altered  
during their time in the block.

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/7 "2007-10-14T19:01:40Z")

</div>

On 10/14/07, SpringFlowers AutumnMoon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 7
> 
> ## 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-forum.com/](http://www.ruby-forum.com/).

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

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/8 "2007-10-14T19:11:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 14, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/9 "2007-10-14T20:28:49Z")

</div>

On 10/14/07, SpringFlowers AutumnMoon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> * * *
> 
> 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+Ruby+1.9#l8](http://eigenclass.org/hiki/Changes+in+Ruby+1.9#l8)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 1:19am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/10 "2007-10-15T01:19:35Z")

</div>

Pat M. 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

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 1:56am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/11 "2007-10-15T01:56:56Z")

</div>

SpringFlowers AutumnMoon wrote:

> I don’t know what software install Ruby 1.9 on my computer
> 
> C:\Users\Mike\ruby-1.9.0-20060415-i386-mswin32

This is a very old version of Ruby 1.9. Please don’t forget that Ruby  
1.9(.0) is still an experimental version, that will move towards the  
final Ruby 1.9.1 in December 2007 (I hope “December 2007” is the correct  
date).

There are newer precompiled versions  
(ruby-1.9.0-20070709-x64-mswin64\_80.zip on  
[http://dl.ambiweb.de/mirrors/ftp.ruby-lang.org/binaries/mswin32/](http://dl.ambiweb.de/mirrors/ftp.ruby-lang.org/binaries/mswin32/)) or  
snapshots which can be compiled (snapshot.tar.gz on  
ftp://ftp.ruby-lang.org/pub/ruby/).

Wolfgang NÃ¡dasi-Donner

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 2:15am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/12 "2007-10-15T02:15:46Z")

</div>

On 10/14/07, SpringFlowers AutumnMoon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> foo(i)  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 10  
> 1

Well, that’s kind of the point of blocks, isn’t it? They close over  
any vars that are currently in scope. Methods on the other hand have  
their own scope. I guess it could be confusing if you thought methods  
and blocks were the same thing…but, um, they’re not.

Pat

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 2:18am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/13 "2007-10-15T02:18:41Z")

</div>

Hi –

On Mon, 15 Oct 2007, SpringFlowers AutumnMoon wrote:

> foo(i)  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 10  
> 1

There’s no relation or connection between the two. The def keyword  
always starts a new local scope; the if keyword doesn’t. There’s no  
reason to expect them to behave the same way as each other, as they  
serve entirely different purposes.

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 1:49am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/14 "2007-10-15T01:49:38Z")

</div>

Rick Denatale wrote:

> This is changing in 1.9
> 
> [eigenclass.org](http://eigenclass.org/hiki/Changes+in+Ruby+1.9#l8)

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

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 3:02am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/15 "2007-10-15T03:02:58Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Mon, 15 Oct 2007, SpringFlowers AutumnMoon wrote:
> 
> > foo(i)  
> > 3  
> > 4  
> > 5  
> > 6  
> > 7  
> > 8  
> > 9  
> > 10  
> > 1
> 
> There’s no relation or connection between the two. The def keyword  
> always starts a new local scope; the if keyword doesn’t. There’s no  
> reason to expect them to behave the same way as each other, as they  
> serve entirely different purposes.

So in a way, think of a code block like a “if” statement? (no new local  
scope)

Come to think about it, since

[1,2,3,4,5].each {|a| p a}

is equivalent to

for a in [1,2,3,4,5]  
p a  
end

so the “for” version clearly doesn’t have a new scope for “a”.

But in the newest Ruby 1.9, they are not equivalent any more?

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 3:22am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/16 "2007-10-15T03:22:25Z")

</div>

SpringFlowers AutumnMoon wrote:

> p a  
> end
> 
> so the “for” version clearly doesn’t have a new scope for “a”.
> 
> But in the newest Ruby 1.9, they are not equivalent any more?

No, they aren’t the same even in Ruby 1.8.x either. With the do…end or  
{…} syntax, any variables assigned in the block, but inexisting at the  
point of the definition of the block, are actually local to the block.

AFAIK, in 1.9, the formal parameters of the block, specified between  
||'s, are considered unconditionally block-local _too_. In 1.8.x, those  
are treated like any other local variables in the block: they refer to  
the same as the enclosing block if pre-existing, and refer to  
block-local variables otherwise.

mortee

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 3:47am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/17 "2007-10-15T03:47:00Z")

</div>

mortee wrote:

> > But in the newest Ruby 1.9, they are not equivalent any more?
> 
> No, they aren’t the same even in Ruby 1.8.x either. With the do…end or  
> {…} syntax, any variables assigned in the block, but inexisting at the  
> point of the definition of the block, are actually local to the block.

I should have pointed out specifically the difference to the for … in  
… end syntax: any local variable created (assigned to) in the latter  
construct become part of the enclosing context, so you can actually  
create new local variables in such a loop for later use.

mortee

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 2:55am UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/18 "2007-10-15T02:55:32Z")

</div>

SpringFlowers AutumnMoon wrote:

> foo(i)  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 10  
> 1

Yes, the def syntax specifically doesn’t take the caller binding, AFAIK.  
If you intend having methods access variables local to where they are  
defined, just use define\_method, which takes a block, with all of its  
peculiarities, like taking the caller binding along with its local  
variables.

mortee

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/19 "2007-10-15T13:07:51Z")

</div>

Hi –

On Mon, 15 Oct 2007, SpringFlowers AutumnMoon wrote:

> > > 6
> 
> So in a way, think of a code block like a “if” statement? (no new local  
> scope)

I think the best thing is to learn about blocks as blocks, and don’t  
worry too much about how much they are or aren’t similar to other  
things. A block has the variables from the local scope in which it was  
created, and can also create its own variables, but the ones it  
creates exist only inside the block. An if statement is truly flat  
with regard to its local scope.

David

---

<div class="post-metadata">

**Author:** ![winterheat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7993a0/32.png) [@winterheat](https://www.ruby-forum.com/u/winterheat)\
**Post date:** [October 15, 2007, 7:50pm UTC](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194/20 "2007-10-15T19:50:11Z")

</div>

SpringFlowers AutumnMoon wrote:

> I thought a iterator with a block is like an nameless function call…

Here’s another example(a modification of the example found in pickaxe2,  
p 106):

if false  
data = “hello”  
end

(1…3).each {data = “goodbye”}

puts data

–output:–  
goodbye

That is equivalent to Java’s call by value–but using references and C++  
reference semantics, and the flat scope is converted to spheric scope  
with assignment semantics applying throughout any call by value passing.  
🙂

[Next page](https://www.ruby-forum.com/t/parameter-in-a-block-is-not-local/117194.md?page=2)
