# Lambda block doesn't work with upto iterator

**URL:** <https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372>\
**Category:** Ruby\
**Created:** [January 17, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372 "2008-01-17T19:44:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ed\_Loyot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@Ed\_Loyot](https://www.ruby-forum.com/u/Ed_Loyot)\
**Post date:** [January 17, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372/1 "2008-01-17T19:44:33Z")

</div>

Hi Folks,

I’m new to Ruby and I’m trying to get a complete understanding of  
blocks. I  
captured some code in a block using lambda and was able to execute the  
code  
with the .time Integer iterator, but I get an exception trying to get it  
to  
work with the .upto iterator. The code is below. Can anyone explain why  
the  
.upto call fails but the .times call succeeds.

Excerpt:

proc1 = lambda do |p1|  
puts “#{p1}”  
end

# This works.

5.times &proc1

# Why does this fail?

0.upto(4) &proc2

Thanks,

Ed

Full Code:

# 

# Experiment with blocks!

# 

# Tried with ruby verion: ruby 1.8.6 (2007-09-24 patchlevel 111)

[i386-mswin32]

# 

# Ed Loyot

# 

proc1 = lambda do |p1|  
puts “#{p1}”  
end

proc2 = Proc.new do |p1|  
puts “#{p1}”  
end

# Make sure the proc works.

puts “Call proc1!”  
proc1.call “112”

puts “Call proc2!”  
proc2.call “212”

# Try it with an iterator.

puts “Call proc1 with times iterator!”  
5.times &proc1

puts “Call proc2 with times iterator!”  
5.times &proc2

# Why does this fail?

begin  
puts “Call proc1 with upto iterator!”  
0.upto(4) &proc1  
rescue StandardError =\> error  
puts "Caught error: " + error  
end

# Why does this fail?

begin  
puts “Call proc2 with upto iterator!”  
0.upto(4) &proc2  
rescue StandardError =\> error  
puts "Caught error: " + error  
end

---

<div class="post-metadata">

**Author:** ![Ed\_Loyot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@Ed\_Loyot](https://www.ruby-forum.com/u/Ed_Loyot)\
**Post date:** [January 17, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372/2 "2008-01-17T19:55:46Z")

</div>

Ed Loyot wrote:

> # Why does this fail?
> 
> 0.upto(4) &proc2

It has to be 0.upto(4,&proc2) or 0.upto 4, &proc2  
In other words: syntax-wise a &-proc is treated like just another  
argument  
(although it has to come last in the argument list), not like a block.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Ed\_Loyot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@Ed\_Loyot](https://www.ruby-forum.com/u/Ed_Loyot)\
**Post date:** [January 17, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372/3 "2008-01-17T19:57:31Z")

</div>

On 17/01/2008, Ed Loyot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # This works.
> 
> 5.times &proc1
> 
> # Why does this fail?
> 
> 0.upto(4) &proc2

0.upto(4,&proc2)

Farrel

---

<div class="post-metadata">

**Author:** ![Ed\_Loyot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/43a26b/32.png) [@Ed\_Loyot](https://www.ruby-forum.com/u/Ed_Loyot)\
**Post date:** [January 17, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/lambda-block-doesnt-work-with-upto-iterator/126372/4 "2008-01-17T19:59:52Z")

</div>

> 5.times &proc2  
> is the same as  
> 5.times(&proc2)

> 0.upto(4) &proc1  
> change to  
> 0.upto(4, &proc1)

It should works.  
I guess that “0.upto(4) &proc1” is parsed by Ruby like bitwise and  
with left operand “0.upto(4)” and right operand “proc1”.

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog
