callbacks in ruby and using yield in resursion

P

Paul

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
 
J

Joel VanderWerf

Paul said:
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.
 
M

maillist

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???
 
J

Joel VanderWerf

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

Brian Candler

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

Robert Klemme

Paul said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top