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?
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