From: Robert Klemme Date: 2004-11-25T17:27:58+09:00 Subject: Re: proc to method "Anders Engstr�m" schrieb im Newsbeitrag news:20041125073108.GA25131@metamorph.gnejs.net... > On Thu, Nov 25, 2004 at 04:18:04PM +0900, itsme213 wrote: > > I have some closure context captured in a proc, and want to create a method > > on a class from that. What is a good way to do this? > > > > Something like the following should work: > > class Test > # The class to add the method to. > end > > # The proc we want to turn into a method > block = proc {|arg| > puts "Proc got #{arg}" > } > > # Define the method > Test.__send__(:define_method, :run_proc, &block) Alternatively: Test.instance_eval { define_method(:run_proc, &block) } > # Make it public > Test.__send__(:public, :run_proc) I think that's not necessary as methods are public by default. > # test it > test = Test.new > test.run_proc "Hello" Kind regards robert