callbacks in ruby and using yield in resursion

Discussion in 'Ruby' started by Paul, Jul 17, 2003.

  1. Paul

    Paul Guest

    Hi,

    How do I pass a ruby function as an argument to another ruby function so
    that it can be used as a callback?

    Also, I have a recursive tree traversing function that I would like to
    use yield with. For example:

    def run(parent,level)
    ....
    yield parent
    ....
    run(child,level+1)#yield????????
    ....
    end

    run(root,0){|node|
    puts node.get_name
    }

    It will not work because i cant use the yield in the recursive call to
    run.

    Any ideas?

    Thanks

    Paul
     
    Paul, Jul 17, 2003
    #1
    1. Advertising

  2. Paul wrote:
    > Hi,
    >
    > How do I pass a ruby function as an argument to another ruby function so
    > that it can be used as a callback?
    >
    > Also, I have a recursive tree traversing function that I would like to
    > use yield with. For example:
    >
    > def run(parent,level)

    def run(parent, level, &block)
    > ....
    > yield parent

    block.call(parent)
    > ....
    > run(child,level+1)#yield????????

    run(child,level+1, &block)
    > ....
    > end
    >
    > run(root,0){|node|
    > puts node.get_name
    > }
    >
    > It will not work because i cant use the yield in the recursive call to
    > run.


    The first use of the "&block" notation converts the caller-supplied
    block to an instance of Proc and stores it in "block".

    The second use of "&block" passes the proc object as the caller-supplied
    block of the recursive call to #run.
     
    Joel VanderWerf, Jul 17, 2003
    #2
    1. Advertising

  3. Paul

    Guest

    It works! Thanks very much. One thing I don't get... what exactly is
    the &block, is it a reserved work, a predefined variable or what???

    On Thu, 2003-07-17 at 13:08, Joel VanderWerf wrote:
    > Paul wrote:
    > > Hi,
    > >
    > > How do I pass a ruby function as an argument to another ruby function so
    > > that it can be used as a callback?
    > >
    > > Also, I have a recursive tree traversing function that I would like to
    > > use yield with. For example:
    > >
    > > def run(parent,level)

    > def run(parent, level, &block)
    > > ....
    > > yield parent

    > block.call(parent)
    > > ....
    > > run(child,level+1)#yield????????

    > run(child,level+1, &block)
    > > ....
    > > end
    > >
    > > run(root,0){|node|
    > > puts node.get_name
    > > }
    > >
    > > It will not work because i cant use the yield in the recursive call to
    > > run.

    >
    > The first use of the "&block" notation converts the caller-supplied
    > block to an instance of Proc and stores it in "block".
    >
    > The second use of "&block" passes the proc object as the caller-supplied
    > block of the recursive call to #run.
    >
     
    , Jul 17, 2003
    #3
  4. wrote:
    > It works! Thanks very much. One thing I don't get... what exactly is
    > the &block, is it a reserved work, a predefined variable or what???


    The & is the only special part, you can call the variable anything.
     
    Joel VanderWerf, Jul 17, 2003
    #4
  5. On Thu, Jul 17, 2003 at 09:49:30AM +0900, Paul wrote:
    > How do I pass a ruby function as an argument to another ruby function so
    > that it can be used as a callback?


    b.method:)a) converts the method 'a' of object 'b' into a Method object,
    which you can pass around and invoke using Method#call

    def meth1(str)
    puts str
    end

    def meth2(m)
    m.call("hello")
    end

    meth2(method:)meth1))

    Regards,

    Brian.
     
    Brian Candler, Jul 17, 2003
    #5
  6. "Paul" <> schrieb im Newsbeitrag
    news:1058402958.4573.7.camel@paulsmachine...
    > Hi,
    >
    > How do I pass a ruby function as an argument to another ruby function so
    > that it can be used as a callback?


    There is no such thing as a function, there are only methods in Ruby. You
    can create Method instances the way Brian showed in his posting. Another
    option is to use a block converted to a Proc:

    def fun2( fun, x )
    fun.call( x, x )
    end

    def add(x,y)
    x+y
    end

    # Method instance (C) 2003 Brian
    fun = method :add
    fun2( fun, 10 )

    # Proc instance
    fun = proc {|x,y| x+y}
    fun2( fun, 10 )

    You can even curry:

    def curry( fun, arg )
    return proc {|x| fun.call(arg, x) }
    end

    fun = curry( method( :add ), 10 )
    fun.call( 5 )

    fun = curry( proc {|x,y| x+y}, 10 )
    fun.call( 5 )

    Regards

    robert
     
    Robert Klemme, Jul 17, 2003
    #6
    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. Replies:
    1
    Views:
    322
    Gabriel Genellina
    Apr 22, 2008
  2. Markus
    Replies:
    1
    Views:
    199
    Mark Hubbart
    Sep 27, 2004
  3. leon breedt
    Replies:
    3
    Views:
    107
    Eric Hodel
    Dec 11, 2004
  4. Alber Eric

    using Proc and yield

    Alber Eric, Dec 15, 2006, in forum: Ruby
    Replies:
    4
    Views:
    135
    George Ogata
    Dec 16, 2006
  5. Michael Edgar
    Replies:
    13
    Views:
    278
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page