Method to Proc

Is there a way to turn a method into a proc stored in a variable?
Something similar to Perl’s & notation.

Say I have a method written and I want to call it indirectly, thru a
proc object. I basically want to get the “address” of the method (or
have a proc object referring to it)

def myfunc(t)
puts t
end

t = &myfunc #this line is perl’s way of doing it
t.call(“hello”)

The idea behind calling functions like these indirectly is so as to
have the ability of the proc object to refer to built methods and/or
on the fly written procs.
Ruby seems to have this functionality, but I cannot find the proper
syntax.

GGarramuno wrote:

Is there a way to turn a method into a proc stored in a variable?
Something similar to Perl’s & notation.

Say I have a method written and I want to call it indirectly, thru a
proc object. I basically want to get the “address” of the method (or
have a proc object referring to it)

def myfunc(t)
puts t
end

t = &myfunc #this line is perl’s way of doing it
t.call(“hello”)

The idea behind calling functions like these indirectly is so as to
have the ability of the proc object to refer to built methods and/or
on the fly written procs.
Ruby seems to have this functionality, but I cannot find the proper
syntax.

Use the “method” method:

t = method( :myfunc )
t.call( “hello” )

···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

hope this helps:
irb(main):001:0> def f; puts ‘ciao’ ; end
=> nil
irb(main):002:0> my=method(:f)
=> #<Method: Object#f>
irb(main):003:0> my.call
ciao
=> nil
irb(main):004:0> myproc=my.to_proc
=> #Proc:0x0280ec78@:4(irb)
irb(main):005:0> myproc.call
ciao
=> nil

and remember you can expand a proc object (or a Method object) into a
block with
&procobject

···

il 4 Jan 2004 11:17:25 -0800, GGarramuno@aol.com (GGarramuno) ha scritto::

Is there a way to turn a method into a proc stored in a variable?
Something similar to Perl’s & notation.

Say I have a method written and I want to call it indirectly, thru a
proc object. I basically want to get the “address” of the method (or
have a proc object referring to it)