# Blocks expected arguments and metaprogramming

**URL:** <https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663>\
**Category:** ruby-talk\
**Created:** [16 September 2007 15:10 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663 "2007-09-16T15:10:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro\_Del\_Gallego](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Pedro\_Del\_Gallego](https://rubytalk.org/u/Pedro_Del_Gallego)\
**Post date:** [16 September 2007 15:10 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663/1 "2007-09-16T15:10:50Z")

</div>

hi all,

in this ([http://pastie.caboo.se/97662](http://pastie.caboo.se/97662) ) code :

module Kernel  
&nbsp;&nbsp;def expected\_argument\_list  
&nbsp;&nbsp;&nbsp;&nbsp;2  
&nbsp;&nbsp;end

&nbsp;&nbsp;def before (name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts :hola  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(name){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if expected\_argument\_list==1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :only\_one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one,:two  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;before :foo do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;puts second  
&nbsp;&nbsp;end

&nbsp;&nbsp;before :foo do |first , second|  
&nbsp;&nbsp;&nbsp;&nbsp;puts second  
&nbsp;&nbsp;end  
end

a = A.new  
a.foo

Does anyone know of a way to determine what argument list was  
specified when the block is created?

i mean i want to implement the function expected\_argument\_list, to  
return the length of the argument list that the block expected.  
Probably is not possible to do that ☹

When are defined the variables of the block, in the class definition  
time, or after the yield call?

Thanks

> **···**
>
> --  
> -------------------------------------  
> Pedro Del Gallego
> 
> Email : [pedro.delgallego@gmail.com](mailto:pedro.delgallego@gmail.com)

---

<div class="post-metadata">

**Author:** ![Wilson\_Bilkovich](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/wilson_bilkovich/32/8247_2.png) [@Wilson\_Bilkovich](https://rubytalk.org/u/Wilson_Bilkovich)\
**Post date:** [16 September 2007 16:56 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663/2 "2007-09-16T16:56:07Z")

</div>

The block arguments are only known at runtime, since you could do this:  
def example  
&nbsp;&nbsp;args =   
&nbsp;&nbsp;rand(15).times {|i| args \<\< i}  
&nbsp;&nbsp;yield \*args  
end

> **···**
>
> On 9/16/07, Pedro Del Gallego \<pedro.delgallego@gmail.com\> wrote:
> 
> > hi all,
> > 
> > in this ([http://pastie.caboo.se/97662](http://pastie.caboo.se/97662) ) code :
> > 
> > module Kernel  
> > &nbsp;&nbsp;def expected\_argument\_list  
> > &nbsp;&nbsp;&nbsp;&nbsp;2  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def before (name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts :hola  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(name){  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if expected\_argument\_list==1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :only\_one  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one,:two  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class A  
> > &nbsp;&nbsp;before :foo do |\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts second  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;before :foo do |first , second|  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts second  
> > &nbsp;&nbsp;end  
> > end
> > 
> > a = A.new  
> > a.foo
> > 
> > Does anyone know of a way to determine what argument list was  
> > specified when the block is created?
> > 
> > i mean i want to implement the function expected\_argument\_list, to  
> > return the length of the argument list that the block expected.  
> > Probably is not possible to do that ☹
> > 
> > When are defined the variables of the block, in the class definition  
> > time, or after the yield call?

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [16 September 2007 17:00 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663/3 "2007-09-16T17:00:10Z")

</div>

def before (name, &blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts :hola  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(name){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if blk.arity==1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :only\_one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one,:two  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;end  
end

It's not quite as simple as that but that should be enough to move you  
forward.

T.

> **···**
>
> On Sep 16, 8:10 am, "Pedro Del Gallego" \<pedro.delgall...@gmail.com\> wrote:
> 
> > hi all,
> > 
> > in this ([http://pastie.caboo.se/97662\](http://pastie.caboo.se/97662%5C)) code :
> > 
> > module Kernel  
> > &nbsp;&nbsp;def expected\_argument\_list  
> > &nbsp;&nbsp;&nbsp;&nbsp;2  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Pedro\_Del\_Gallego](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Pedro\_Del\_Gallego](https://rubytalk.org/u/Pedro_Del_Gallego)\
**Post date:** [16 September 2007 17:40 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663/4 "2007-09-16T17:40:03Z")

</div>

> \> hi all,  
> \>  
> \> in this ([http://pastie.caboo.se/97662\](http://pastie.caboo.se/97662%5C)) code :  
> \>  
> \> module Kernel  
> \> def expected\_argument\_list  
> \> 2  
> \> end  
> \>  
> &nbsp;&nbsp;&nbsp;def before (name, &blk)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts :hola  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(name){  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if blk.arity==1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :only\_one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one,:two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;end  
> end
> 
> It's not quite as simple as that but that should be enough to move you  
> forward.

Yes thats enough for me. I didnt now that the block was a Proc class.

Thanks.

> **···**
>
> > On Sep 16, 8:10 am, "Pedro Del Gallego" \<pedro.delgall...@gmail.com\> \> wrote:
> 
> --  
> -------------------------------------  
> Pedro Del Gallego
> 
> Email : pedro.delgallego@gmail.com

---

<div class="post-metadata">

**Author:** ![Pedro\_Del\_Gallego](https://avatars.discourse-cdn.com/v4/letter/p/a4c791/32.png) [@Pedro\_Del\_Gallego](https://rubytalk.org/u/Pedro_Del_Gallego)\
**Post date:** [16 September 2007 18:06 UTC](https://rubytalk.org/t/blocks-expected-arguments-and-metaprogramming/40663/5 "2007-09-16T18:06:41Z")

</div>

BTW, its works perfectly, with that i can decide what parameter return  
in advances, (but, yet, I dont know is a good idea or not)

Thanks

module Kernel  
&nbsp;&nbsp;def before (name, &block)  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method(name) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block.arity == 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :only\_one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif block.arity == 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one,:two  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif block.arity == -2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield :one, :a , :splater, :argument  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;before :foo do |first|  
&nbsp;&nbsp;&nbsp;&nbsp;puts first  
&nbsp;&nbsp;end  
end

a = A.new  
a.foo

class A  
&nbsp;&nbsp;before :foo do |first , second|  
&nbsp;&nbsp;&nbsp;&nbsp;puts second  
&nbsp;&nbsp;end  
end

a2 = A.new  
a2.foo

class A  
&nbsp;&nbsp;before :foo do |first , \*splat\_arg|  
&nbsp;&nbsp;&nbsp;&nbsp;puts splat\_arg  
&nbsp;&nbsp;end  
end

a3 = A.new  
a3.foo

> **···**
>
> 2007/9/16, Pedro Del Gallego \<pedro.delgallego@gmail.com\>:
> 
> > \>  
> > \> On Sep 16, 8:10 am, "Pedro Del Gallego" \<pedro.delgall...@gmail.com\> \> \> wrote:  
> > \> \> hi all,  
> > \> \>  
> > \> \> in this ([http://pastie.caboo.se/97662\](http://pastie.caboo.se/97662%5C)) code :  
> > \> \>  
> > \> \> module Kernel  
> > \> \> def expected\_argument\_list  
> > \> \> 2  
> > \> \> end  
> > \> \>  
> > \> def before (name, &blk)  
> > \> puts :hola  
> > \> define\_method(name){  
> > \> if blk.arity==1  
> > \> yield :only\_one  
> > \> else  
> > \> yield :one,:two  
> > \> end  
> > \> }  
> > \> end  
> > \> end  
> > \>  
> > \> It's not quite as simple as that but that should be enough to move you  
> > \> forward.
> > 
> > Yes thats enough for me. I didnt now that the block was a Proc class.
> > 
> > Thanks.
> > 
> > --  
> > -------------------------------------  
> > Pedro Del Gallego
> > 
> > Email : pedro.delgallego@gmail.com
> 
> --  
> -------------------------------------  
> Pedro Del Gallego
> 
> Email : pedro.delgallego@gmail.com
