From: Peter Date: 2004-12-07T04:14:26+09:00 Subject: Re: [ANN] patch to "make def return something useful" > IMO, that's an implementation detail that shouldn't be exposed at the > metaprogramming level. Part of it is exposed indirectly. For example the class matters when binding a method, and there could be applications that want to check this class before binding an UnboundMethod. > Yes, but the whole point of having def return something useful is to > make it easier for someone to use this information at the Ruby level, > not at the Ruby core level. Returning a Method or UnboundMethod is, > ultimately, useless for meta-programming. I'll repeat my disclaimer here: I made this patch, but that does not mean that it shows how I think it should be. I've taken an idea from an RCR and combined it with an idea I've seen come up at suby-muse and ruby-talk. I'm not fully happy with it myself. A Method or UnboundMethod is detached from the class it was taken from, and that class nor its name really matters after it's been detached from the class, except in tiny details, such as an UnboundMethod that can only be bound to an object with the original class of the method in its ancestors. Because of this, the implementation actually needs to check whether that method is still in the original class and whether it is not redefined, resulting in an exception when it is gone or redefined. In a way, even if Method provided methods for getting the method name and class, it is the wrong information to pass to Module#(public|protected|private) because the info contained in Method may be out of date. But what should it return then? A method name only isn't always sufficient, e.g., for class methods. Returning an array containing the name of the method and the name of the class isn't sufficient either because classes can be anonymous. Returning the class itself instead of just the name doesn't work for class methods, so for class methods (and singleton methods in general), it should return the singleton class. So the def in "class A ; def foo; end ; end" should return [:foo, A] and "def a.foo ; end" should return [:foo, class << a ; self ; end]. That would work, but I'd rather have def return something that I can ask for its name or attached class instead of element 0 and element 1. A Struct of some kind maybe? Peter