From: Ben Tilly Date: 2001-02-08T15:38:17+09:00 Subject: [ruby-talk:10549] Giving a Proc utility methods? Here is my situation. I have a class, call it Foo. Foo has an attribute which is a callback. I want to have this callback be in the caller's context, but there are some utility functions that I want to make readily available. Note that I am well aware of how to make this work with an instance eval, but I don't want to do that because I want to preserve the closure. My first naive thought was to make those functions methods in my current class. Which doesn't work since I have an object of another class. (Duh, what did I expect?) My second, less naive, thought was to make those functions private methods of the object. That fails in a way which is somewhat reasonable, I guess, but still makes me aware that I don't understand how it is working internally. In particular my method is called but generates an exception because it is a private method. But I can't seem to figure out how to say that I really want it to be public after all. :-( I guess I could make them available in a module to be mixed in by anyone wishing to use the class, but I was hoping that there was a nicer approach. Here is a sample of what I hoped would work in the hopes that there is a simple solution I have missed: class Foo attr :err def initialize (&meth) # TRY to add the method? def meth.fail (msg) puts msg end @err = meth end def throw_err (msg) @err[msg] end end # I want to preserve this closure... bar = "testing..." # Example showing the callback works my_foo = Foo.new {|msg| puts msg + bar} my_foo.throw_err "Trial 1: " # Example of what I *wanted* to do... my_foo = Foo.new {|msg| self.fail msg + bar} my_foo.throw_err "Trial 2: " (What I really wanted to do was not puts the error, but rather throw it, and I didn't want synchronization problems between what I label the throw and the catch so I only wanted to define those once.) Cheers, Ben _________________________________________________________________ Get your FREE download of MSN Explorer at http://explorer.msn.com