Paul23
(Paul)
17 July 2003 00:49
1
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 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 .
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.
···
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?
Robert
(Robert)
17 July 2003 13:36
4
“Paul” webmaster@startlinux.co.nz 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
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 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.
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
···
On Thu, 17 Jul 2003 10:59:31 +0900, maillist@bestworldweb.homelinux.com 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???
–
austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.07.16
* 23:44:52