I have a model that references and instance variable that is an instance of another class. I would like to delegate missing methods to that variable like this:
def method_missing(method_name, *args, &block)
if @other_class_instance.respond_to?(method_name)
@other_class_instance.send(method_name, *args)
else
super
end
end
However, in the case that @other_class_instance doesn't respond, I'd like the app to terminate and get a full backtrace as a normal NoMethodError would. Instead, I just get one line error messages such as:
#<NoMethodError: undefined method `my_method' for #<MyClass:0x00000009022fc0>>
I've read in several places that if super isn't there, it will mess up Ruby's method lookup.
What am I missing so that if other_class_instance
doesn't respond to the method, it will behave as if no method_missing
method was created?
Update
Logan's answer takes care of this problem under the condition that the OtherInstance class does not also have a method_missing
defined. In the case that it does (say for a Rails model), you could do something like the following:
begin
raise NoMethodError.new method_name
rescue => e
logger.fatal e
logger.fatal e.backtrace
super
end