# Blocks expected arguments and metaprogramming

**URL:** <https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726 "2007-09-17T13:24:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726/1 "2007-09-17T13:24:49Z")

</div>

hi all,

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

module Kernel  
def expected\_argument\_list  
2  
end

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

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

before :foo do |first , second|  
puts second  
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 G.

Email : removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726/2 "2007-09-17T13:25:43Z")

</div>

On Sep 16, 8:10 am, “Pedro Del G.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> hi all,
> 
> in this ([Parked at Loopia](http://pastie.caboo.se/97662)) 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.

T.

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [September 25, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726/3 "2007-09-25T23:01:46Z")

</div>

> > 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 G.

Email : removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726/4 "2007-09-25T23:05:36Z")

</div>

On 9/16/07, Pedro Del G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> puts :hola
> 
> ```
> 
> class A  
> a.foo

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

---

<div class="post-metadata">

**Author:** ![pedro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/919ad9/32.png) [@pedro](https://www.ruby-forum.com/u/pedro)\
**Post date:** [September 25, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/blocks-expected-arguments-and-metaprogramming/114726/5 "2007-09-25T23:00:17Z")

</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  
def before (name, &block)  
define\_method(name) do  
if block.arity == 1  
yield :only\_one  
elsif block.arity == 2  
yield :one,:two  
elsif block.arity == -2  
yield :one, :a , :splater, :argument  
end  
end  
end  
end

class A  
before :foo do |first|  
puts first  
end  
end

a = A.new  
a.foo

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

a2 = A.new  
a2.foo

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

a3 = A.new  
a3.foo

2007/9/16, Pedro Del G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > end  
> > > end  
> > > –
> 
> * * *
> 
> Pedro Del G.
> 
> Email : removed\_email\_address@domain.invalid

## –

Pedro Del G.

Email : removed\_email\_address@domain.invalid
