# Why I can not use my own blocks in 1.9?

**URL:** <https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867>\
**Category:** Ruby\
**Created:** [October 7, 2009, 9:31am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867 "2009-10-07T09:31:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 7, 2009, 9:31am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/1 "2009-10-07T09:31:16Z")

</div>

Example:  
1.8.7:  
a = lambda {|x,y| x \<=\> y}  
[1,2,3].max(&a) # =\> 3  
1.9.1:  
z = lambda {|x,y| x \<=\> y}  
[3,2,1].max(&z) # =\> ArgumentError: wrong number of arguments (1 for 2)

wtf?

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 7, 2009, 9:32am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/2 "2009-10-07T09:32:56Z")

</div>

z = lambda {|x| x[0] \<=\> x[1]}  
[3,2,1].max(&z) # =\> 3

it works in 1.9!

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 7, 2009, 12:49pm UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/3 "2009-10-07T12:49:07Z")

</div>

Ivan S. wrote:

> Example:  
> 1.8.7:  
> a = lambda {|x,y| x \<=\> y}  
> [1,2,3].max(&a) # =\> 3  
> 1.9.1:  
> z = lambda {|x,y| x \<=\> y}  
> [3,2,1].max(&z) # =\> ArgumentError: wrong number of arguments (1 for 2)
> 
> wtf?

lambda does arity checking in 1.9 as opposed to proc that doesn’t (they  
aren’t synonymous anymore). You can work around this problem by using  
this definition for the order predicate:

z = proc { |x,y| x \<=\> y}

1.9 obviously just passes an array to the block via rb\_yield which used  
to make sense in 1.8. But now using rb\_yield\_values probably would be  
better to give users a hint about the expected predicate.

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 7, 2009, 10:31pm UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/4 "2009-10-07T22:31:06Z")

</div>

On 10/07/2009 12:48 PM, Florian F. wrote:

> > 
> 
> lambda does arity checking in 1.9 as opposed to proc that doesn’t (they  
> aren’t synonymous anymore). You can work around this problem by using  
> this definition for the order predicate:
> 
> z = proc { |x,y| x \<=\> y}
> 
> 1.9 obviously just passes an array to the block via rb\_yield which used  
> to make sense in 1.8. But now using rb\_yield\_values probably would be  
> better to give users a hint about the expected predicate.

This has worked in 1.9.0:

irb(main):001:0\> RUBY\_VERSION  
=\> “1.9.0”  
irb(main):002:0\> l = lambda {|a,b| a \<=\> b}  
=\> #\<Proc:0x9522a5c@(irb):2 (lambda)\>  
irb(main):003:0\> [3,2,1].sort(&l)  
=\> [1, 2, 3]

Darn, I wasn’t aware that the 1.9 on ubuntu is that old. Looks like I  
need to do a manual build.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 8, 2009, 3:15am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/5 "2009-10-08T03:15:19Z")

</div>

Florian F. wrote:

> Ivan S. wrote:
> 
> > Example:  
> > 1.8.7:  
> > a = lambda {|x,y| x \<=\> y}  
> > [1,2,3].max(&a) # =\> 3  
> > 1.9.1:  
> > z = lambda {|x,y| x \<=\> y}  
> > [3,2,1].max(&z) # =\> ArgumentError: wrong number of arguments (1 for 2)
> > 
> > wtf?
> 
> lambda does arity checking in 1.9 as opposed to proc that doesn’t (they  
> aren’t synonymous anymore). You can work around this problem by using  
> this definition for the order predicate:
> 
> z = proc { |x,y| x \<=\> y}
> 
> 1.9 obviously just passes an array to the block via rb\_yield which used  
> to make sense in 1.8. But now using rb\_yield\_values probably would be  
> better to give users a hint about the expected predicate.

How is it that both the following calls to test() work:

def test  
yield [10, “red”]  
end

test {|x| p x}  
puts

test do |x, y|  
p x  
puts “–”  
p y  
end

–output:–  
[10, “red”]

## 10

“red”

After all, this doesn’t work:

def test(x, y)  
p x  
p y  
end

test([10, “red”])

–output:–  
`test’: wrong number of arguments (1 for 2) (ArgumentError)  
from r1test.rb:19

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 8, 2009, 4:30am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/6 "2009-10-08T04:30:35Z")

</div>

Hi –

On Thu, 8 Oct 2009, 7stud – wrote:

> p x
> 
> –output:–  
> `test’: wrong number of arguments (1 for 2) (ArgumentError)  
> from r1test.rb:19

Lambda-flavored Proc objects are fussier about arity than  
non-lambda-flavored ones. For example:

def test  
yield(1)  
end

test(&proc {|x,y| puts “Will get here in 1.9.1”})  
test(&lambda {|x,y| puts “Won’t get here in 1.9.1”})

Output:

Will get here in 1.9.1  
-:2:in `test’: wrong number of arguments (1 for 2) (ArgumentError)

So the lambda is more method-like in this particular respect than the  
non-lambda Proc (since methods, likewise, care about arity).

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![ivanstorck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e99b99/32.png) [@ivanstorck](https://www.ruby-forum.com/u/ivanstorck)\
**Post date:** [October 8, 2009, 6:15am UTC](https://www.ruby-forum.com/t/why-i-can-not-use-my-own-blocks-in-1-9/176867/7 "2009-10-08T06:15:01Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Thu, 8 Oct 2009, 7stud – wrote:
> 
> > p x
> > 
> > –output:–  
> > `test’: wrong number of arguments (1 for 2) (ArgumentError)  
> > from r1test.rb:19
> 
> Lambda-flavored Proc objects are fussier about arity than  
> non-lambda-flavored ones. For example:
> 
> def test  
> yield(1)  
> end
> 
> test(&proc {|x,y| puts “Will get here in 1.9.1”})  
> test(&lambda {|x,y| puts “Won’t get here in 1.9.1”})
> 
> Output:
> 
> Will get here in 1.9.1  
> -:2:in `test’: wrong number of arguments (1 for 2) (ArgumentError)
> 
> So the lambda is more method-like in this particular respect than the  
> non-lambda Proc (since methods, likewise, care about arity).

Ok. I get that. What I don’t understand is how a yield statement  
decides how to distribute its arguments to the parameter variables  
specified for a block. In a method call, you have to use \* to ‘explode’  
an array into its individual elements, which will cause the individual  
elements to be assigned to the various parameter variables. yield seems  
to act differently. In one case, it assigns a whole array to a single  
parameter variable, and in another case it explodes the array into its  
individual elements and assigns the elements to the parameter variables.
