Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Block that takes a block

1 view
Skip to first unread message

Edward Faulkner

unread,
Sep 20, 2005, 10:57:03 AM9/20/05
to

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

signature.asc

Brian Mitchell

unread,
Sep 20, 2005, 11:03:03 AM9/20/05
to
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.1 (GNU/Linux)
>
> iD8DBQFDMCMJnhUz11p9MSARAqUKAJ44DiESJ4rZuLJOpLCfvMeWV9YE2wCeJnsP
> 0XRKv+gGaRZYL+qzRjjJHBw=
> =kqJd
> -----END PGP SIGNATURE-----

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.


0 new messages