define_method to define a method to pass a block to another method

Discussion in 'Ruby' started by John J. Franey, Jun 13, 2007.

  1. I would like to define the following method, func, using define_method
    instead of def:

    def func(i, &block)
    wblock(i, &block)
    end

    def wblock(i, &block)
    yield(i)
    end


    The following didn't work:

    # syntax error
    define_method('func') do |i,&block|
    wblock(i, &block)
    end

    # ruby expects a second parameter, not a block
    define_method('func') do |i,block|
    wblock(i, &block)
    end

    Any ideas?

    Thanks
    --
    View this message in context: http://www.nabble.com/define_method...ck-to-another-method-tf3914663.html#a11099403
    Sent from the ruby-talk mailing list archive at Nabble.com.
     
    John J. Franey, Jun 13, 2007
    #1
    1. Advertising

  2. John J. Franey

    Paul Novak Guest

    On Jun 13, 9:40 am, "John J. Franey" <> wrote:
    > I would like to define the following method, func, using define_method
    > instead of def:
    >
    > def func(i, &block)
    > wblock(i, &block)
    > end
    >
    > def wblock(i, &block)
    > yield(i)
    > end
    >
    > The following didn't work:
    >
    > # syntax error
    > define_method('func') do |i,&block|
    > wblock(i, &block)
    > end
    >
    > # ruby expects a second parameter, not a block
    > define_method('func') do |i,block|
    > wblock(i, &block)
    > end
    >
    > Any ideas?
    >
    > Thanks
    > --
    > View this message in context:http://www.nabble.com/define_method-to-define-a-method-to-pass-a-bloc...
    > Sent from the ruby-talk mailing list archive at Nabble.com.


    Sorry I don't have time to work out a more complete answer, but check
    this out: http://innig.net/software/ruby/closures-in-ruby.rb it is
    the best exploration of Ruby closures that I've seen.

    Regards,

    Paul.
     
    Paul Novak, Jun 13, 2007
    #2
    1. Advertising

  3. Re: define_method to define a method to pass a block to anothermethod

    Paul Novak wrote:
    >
    > On Jun 13, 9:40 am, "John J. Franey" <> wrote:
    >> I would like to define the following method, func, using define_method
    >> instead of def:
    >>
    >> def func(i, &block)
    >> wblock(i, &block)
    >> end
    >>
    >> def wblock(i, &block)
    >> yield(i)
    >> end
    >>
    >> The following didn't work:
    >>
    >> # syntax error
    >> define_method('func') do |i,&block|
    >> wblock(i, &block)
    >> end
    >>
    >> # ruby expects a second parameter, not a block
    >> define_method('func') do |i,block|
    >> wblock(i, &block)
    >> end
    >>
    >> Any ideas?
    >>
    >> Thanks
    >> --
    >> View this message in
    >> context:http://www.nabble.com/define_method-to-define-a-method-to-pass-a-bloc...
    >> Sent from the ruby-talk mailing list archive at Nabble.com.

    >
    > Sorry I don't have time to work out a more complete answer, but check
    > this out: http://innig.net/software/ruby/closures-in-ruby.rb it is
    > the best exploration of Ruby closures that I've seen.
    >
    >


    Paul,

    Thanks, that's quite enough :) . I read through this and I agree its quite
    an extensive exploration of 'closure-things'.

    I think the answer to my question, then is: define_method cannot define a
    method that accepts a block passed implicitly or using the '&'.
    define_method can define a method that explicitly accepts a 'closure-thing':

    define_method('func') do |p1, closure|
    ...
    closure.call ..
    ..
    end

    used this way:

    obj.func(1, lambda( {puts "hello"}))

    NOT this way:

    obj.func(1) {puts "hello"} # the way I wanted

    Regards,
    John



    --
    View this message in context: http://www.nabble.com/define_method...ck-to-another-method-tf3914663.html#a11125737
    Sent from the ruby-talk mailing list archive at Nabble.com.
     
    John J. Franey, Jun 14, 2007
    #3
  4. John J. Franey

    George Guest

    On 6/15/07, John J. Franey <> wrote:
    > I think the answer to my question, then is: define_method cannot define a
    > method that accepts a block passed implicitly or using the '&'.


    Correct.

    In ruby 1.8, blocks can't accept a block. This feature has been added in 1.9.

    If you need to dynamically define methods which take blocks in 1.8, I
    think you'll need to use #class_eval / #module_eval either with a
    block with defs in it (less useful, since locals outside the block
    aren't accesible inside the defs), or with a string argument (with the
    usual caveats about evalling strings which might be generated from
    user input).

    George.
     
    George, Jun 15, 2007
    #4
    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. morrell
    Replies:
    1
    Views:
    990
    roy axenov
    Oct 10, 2006
  2. trans.  (T. Onoma)

    define_method, proc with block ?

    trans. (T. Onoma), Oct 1, 2004, in forum: Ruby
    Replies:
    6
    Views:
    146
    trans. (T. Onoma)
    Oct 1, 2004
  3. Erik Veenstra

    define_method with a &block?

    Erik Veenstra, Feb 5, 2006, in forum: Ruby
    Replies:
    6
    Views:
    176
    Joel VanderWerf
    Feb 5, 2006
  4. Leslie Viljoen

    define_method with block

    Leslie Viljoen, Jun 27, 2008, in forum: Ruby
    Replies:
    7
    Views:
    135
    Leslie Viljoen
    Jun 27, 2008
  5. James Coglan

    Passing a block with define_method

    James Coglan, Aug 26, 2008, in forum: Ruby
    Replies:
    12
    Views:
    221
    steve
    Oct 29, 2008
Loading...

Share This Page