From: Paul Brannan Date: 2002-03-19T01:52:07+09:00 Subject: Re: instance_respond_to? On Mon, Mar 18, 2002 at 03:36:44AM +0900, Thomas Sondergaard wrote: > Shouldn't module have an instance_respond_to? method? I'm not sure why it is needed, when Object#respond_to? works 99% of the time. > I'd like to use it in a state pattern, where I'd like to do something > like: > > class ClosedState > def method_missing(aSymbol, *args) > raiseClosedError if Impl.instance_respond_to? aSymbol > super > end > end > > For now I will add it myself. Shouldn't the following do fine? Why add it to Module? Modifying built-in classes is something that should only be done when absolutely necessary. IMO, you should make this a standalone method. > class Module > def instance_respond_to? aSymbol > return Module.public_instance_methods.include?(aSymbol.to_s) > end > end This will work, but I think it's an O(n) operation. Paul