Block that takes a block

E

Edward Faulkner

--3MwIy2ne0vdjdPXF
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable


How can I make a block that takes a block argument? This doesn't parse:

myproc =3D 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. =20

For example, the 'before' method below works fine, until you apply it
to a method that takes a block. =20

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 =3D method(method_name)
metaclass.instance_eval {
define_method(method_name){|*args|
blk.call(*args)
oldblk.call(*args)
}
}
end
end

thanks,
Ed

--3MwIy2ne0vdjdPXF
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDMCMJnhUz11p9MSARAqUKAJ44DiESJ4rZuLJOpLCfvMeWV9YE2wCeJnsP
0XRKv+gGaRZYL+qzRjjJHBw=
=kqJd
-----END PGP SIGNATURE-----

--3MwIy2ne0vdjdPXF--
 
B

Brian Mitchell

How can I make a block that takes a block argument? This doesn't parse:

myproc =3D 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 =3D 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=3D
=3DkqJd
-----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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top