Professional Web Applications Themes

callbacks in ruby and using yield in resursion - Ruby

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...

  1. #1

    Default callbacks in ruby and using yield in resursion

    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 Guest

  2. #2

    Default Re: callbacks in ruby and using yield in resursion

    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 Guest

  3. #3

    Default Re: callbacks in ruby and using yield in resursion

    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.
    >

    maillist@bestworldweb.homelinux.com Guest

  4. #4

    Default Re: callbacks in ruby and using yield in resursion

    [email]maillistbestworldweb.homelinux.com[/email] 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 Guest

  5. #5

    Default Re: callbacks in ruby and using yield in resursion

    On Thu, 17 Jul 2003 10:59:31 +0900, [email]maillistbestworldweb.homelinux.com[/email]
    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???
    No, it's just another variable. The only thing special here is the '&' and
    that it must be the last variable. I think that it's more or less
    conventional to call block variables &block, but that's convention.

    Passed blocks can be called with yield (although not by name, afaict), by
    &block.call, or even eval'ed (I have instance_eval &block in a number of
    places in my modules).

    Implicit blocks aren't named and can only be called with yield and detected
    with Kernel#block_given?

    -austin
    --
    austin ziegler * [email]austinhalostatue.ca[/email] * Toronto, ON, Canada
    software designer * pragmatic programmer * 2003.07.16
    * 23:44:52




    Austin Ziegler Guest

  6. #6

    Default Re: callbacks in ruby and using yield in resursion

    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 Guest

  7. #7

    Default Re: callbacks in ruby and using yield in resursion


    "Paul" <webmasterstartlinux.co.nz> schrieb im Newsbeitrag
    news:1058402958.4573.7.camelpaulsmachine...
    > 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 Guest

Similar Threads

  1. break in yield/block
    By matt in forum Ruby
    Replies: 7
    Last Post: November 21st, 08:56 PM
  2. Thoughts on yield
    By Nolan J. Darilek in forum Ruby
    Replies: 16
    Last Post: September 30th, 05:05 PM
  3. Summary: callbacks (called in C embedding or extending Ruby)
    By Basile STARYNKEVITCH in forum Ruby
    Replies: 0
    Last Post: September 2nd, 03:29 PM
  4. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
    By guillaume.pierronnet@ratp.fr in forum Ruby
    Replies: 0
    Last Post: August 31st, 11:57 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139