Block that takes a block

Discussion in 'Ruby' started by Edward Faulkner, Sep 20, 2005.

  1. --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--
     
    Edward Faulkner, Sep 20, 2005
    #1
    1. Advertising

  2. On 9/20/05, Edward Faulkner <> wrote:
    >
    > 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.
     
    Brian Mitchell, Sep 20, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Rossney
    Replies:
    0
    Views:
    2,109
    Robert Rossney
    Sep 1, 2003
  2. Chuck Swart

    bata takes too long with sun

    Chuck Swart, Sep 13, 2003, in forum: VHDL
    Replies:
    1
    Views:
    480
    Chuck Swart
    Sep 13, 2003
  3. morrell
    Replies:
    1
    Views:
    975
    roy axenov
    Oct 10, 2006
  4. Bil Kleb
    Replies:
    5
    Views:
    128
  5. Lee Griffiths
    Replies:
    8
    Views:
    141
    Roger Pack
    Feb 13, 2011
Loading...

Share This Page