From: "Anders Engström" Date: 2004-11-25T17:48:34+09:00 Subject: Re: proc to method On Thu, Nov 25, 2004 at 05:27:58PM +0900, Robert Klemme wrote: > > "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. > I didn't think that either... but when I ran the code without the 'publification call' I got: proc_to_meth.rb:13: private method `run_proc' called for # (NoMethodError) This is on Linux metamorph 2.6.6 #1 Mon May 31 12:59:53 CEST 2004 i686 GNU/Linux ruby 1.8.2 (2004-11-03) [i386-linux] //Anders -- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anders Engstr�m aengstrom@gnejs.net . http://www.gnejs.net PGP-Key: ED010E7F . [Your mind is like an umbrella. It doesn't work unless you open it.]