How to get a reference to a block (when no explicit block parameteris used?)

Discussion in 'Ruby' started by Kenneth McDonald, Nov 26, 2008.

  1. In a function, I can find out if a block was given using block_given?,
    but there's no way I can find to get hold of that block if it's there--
    I can only yield to it, so far as I can tell. I know there has to be a
    method somewhere to do this, could some kind soul enlighten me?

    Many thanks,
    Ken
     
    Kenneth McDonald, Nov 26, 2008
    #1
    1. Advertising

  2. Re: How to get a reference to a block (when no explicit blockparameteris used?)

    Kenneth McDonald <> wrote:

    > In a function, I can find out if a block was given using block_given?,
    > but there's no way I can find to get hold of that block if it's there--
    > I can only yield to it, so far as I can tell. I know there has to be a
    > method somewhere to do this, could some kind soul enlighten me?
    >
    > Many thanks,
    > Ken


    def my_meth param, &block
    p block
    end

    Regards,
    Jan Friedrich
     
    Jan Friedrich, Nov 26, 2008
    #2
    1. Advertising

  3. Kenneth McDonald

    Pit Capitain Guest

    Pit Capitain, Nov 26, 2008
    #3
  4. Re: How to get a reference to a block (when no explicit blockparameter is used?)

    Unfortunately, that produces a method that _requires_ a block--I want
    one where the block is optional. But thanks for the feedback.

    Ken


    On Nov 26, 2008, at 10:30 AM, Jan Friedrich wrote:

    > Kenneth McDonald <> wrote:
    >
    >> In a function, I can find out if a block was given using
    >> block_given?,
    >> but there's no way I can find to get hold of that block if it's
    >> there--
    >> I can only yield to it, so far as I can tell. I know there has to
    >> be a
    >> method somewhere to do this, could some kind soul enlighten me?
    >>
    >> Many thanks,
    >> Ken

    >
    > def my_meth param, &block
    > p block
    > end
    >
    > Regards,
    > Jan Friedrich
    >
     
    Kenneth McDonald, Nov 26, 2008
    #4
  5. Kenneth McDonald

    James Coglan Guest

    Re: How to get a reference to a block (when no explicit blockparameter is used?)

    [Note: parts of this message were removed to make it a legal post.]

    2008/11/26 Jan Friedrich <>

    > Kenneth McDonald <> wrote:
    >
    > > In a function, I can find out if a block was given using block_given?,
    > > but there's no way I can find to get hold of that block if it's there--
    > > I can only yield to it, so far as I can tell. I know there has to be a
    > > method somewhere to do this, could some kind soul enlighten me?
    > >
    > > Many thanks,
    > > Ken

    >
    > def my_meth param, &block
    > p block
    > end




    The way I understood the question was as follows:

    def my_method(arg)
    if block_given?
    yield(arg)
    end
    end

    How to get a reference to the given block in this situation? This is
    especially true if you use define_method, which cannot specify a block param
    (at least not in 1.8). Any suggestions?
     
    James Coglan, Nov 26, 2008
    #5
  6. Re: How to get a reference to a block (when no explicit blockparameter is used?)

    Kenneth McDonald wrote:
    > Unfortunately, that produces a method that _requires_ a block


    No, it does not.

    HTH,
    Sebastian Hungerecker
    =2D-=20
    NP: Die =C4rzte - Mondo Bondage
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 26, 2008
    #6
  7. Re: How to get a reference to a block (when no explicit block parameter is used?)

    On Nov 26, 10:54=A0am, Kenneth McDonald
    <> wrote:
    > Unfortunately, that produces a method that _requires_ a block--I want
    > one where the block is optional. But thanks for the feedback.


    Did you try it?

    >> my_meth(5) { puts 'hi' }

    #<Proc:0x0006ae28@(irb):10>
    >> my_meth(5)

    nil

    --
    -yossef
     
    Yossef Mendelssohn, Nov 26, 2008
    #7
  8. Re: How to get a reference to a block (when no explicit block parameter is used?)

    Whoops, apologies to all. Didn't realize &b arguments were optional.

    K


    On Nov 26, 2008, at 11:00 AM, Yossef Mendelssohn wrote:

    > On Nov 26, 10:54 am, Kenneth McDonald
    > <> wrote:
    >> Unfortunately, that produces a method that _requires_ a block--I want
    >> one where the block is optional. But thanks for the feedback.

    >
    > Did you try it?
    >
    >>> my_meth(5) { puts 'hi' }

    > #<Proc:0x0006ae28@(irb):10>
    >>> my_meth(5)

    > nil
    >
    > --
    > -yossef
    >
     
    Kenneth McDonald, Nov 27, 2008
    #8
    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. J.T. Conklin
    Replies:
    1
    Views:
    467
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    527
    Shezan Baig
    Jan 30, 2005
  3. morrell
    Replies:
    1
    Views:
    1,005
    roy axenov
    Oct 10, 2006
  4. Replies:
    1
    Views:
    618
    Salt_Peter
    Dec 25, 2006
  5. Wolfgang Nádasi-Donner
    Replies:
    0
    Views:
    141
    Wolfgang Nádasi-Donner
    May 31, 2007
Loading...

Share This Page