Afficher un message
Vieux 16/09/2007, 17h56   #2
Wilson Bilkovich
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: blocks expected arguments and metaprogramming

On 9/16/07, Pedro Del Gallego <pedro.delgallego@gmail.com> wrote:
> hi all,
>
> in this (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 nly_one
> else
> yield ne,: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?
>


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

  Réponse avec citation
 
Page generated in 0,06229 seconds with 9 queries