How can I make a block that takes a block argument? This doesn't parse:
myproc = proc{|&blk| [1,2,3].each(&blk)}
The main reason I want to do this is to create a method with
'define_method' that takes a block argument.
For example, the 'before' method below works fine, until you apply it
to a method that takes a block.
class Object
# Is there a built-in way to do this?
def metaclass; class << self; self; end; end
# Call blk before each invocation of method_name
def before(method_name, &blk)
oldblk = method(method_name)
metaclass.instance_eval {
define_method(method_name){|*args|
blk.call(*args)
oldblk.call(*args)
}
}
end
end
thanks,
Ed
I believe this is a limitation in the current 1.8 series. This has
been lifted in HEAD.
You might want to consider using a smart intermediate method that is a
wrapper around the block which can safely take an instance of Proc.
Brian.
···
On 9/20/05, Edward Faulkner <ef@alum.mit.edu> wrote:
How can I make a block that takes a block argument? This doesn't parse:
myproc = proc{|&blk| [1,2,3].each(&blk)}
The main reason I want to do this is to create a method with
'define_method' that takes a block argument.
For example, the 'before' method below works fine, until you apply it
to a method that takes a block.
class Object
# Is there a built-in way to do this?
def metaclass; class << self; self; end; end
# Call blk before each invocation of method_name
def before(method_name, &blk)
oldblk = method(method_name)
metaclass.instance_eval {
define_method(method_name){|*args|
blk.call(*args)
oldblk.call(*args)
}
}
end
end
thanks,
Ed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDMCMJnhUz11p9MSARAqUKAJ44DiESJ4rZuLJOpLCfvMeWV9YE2wCeJnsP
0XRKv+gGaRZYL+qzRjjJHBw=
=kqJd
-----END PGP SIGNATURE-----