How can an exception class obtain the object that raised it?
I want one of my exception classes to be able to obtain the object in
whose context the exception was raised. For example, in the
semi-pseudocode
class Foo
def zorch!
raise MyException.new("d'oh!")
end
end
c = Foo.new
c.zorch!
I want MyException#initialize to be able to obtain the object c.
This can probably be done somehow using self.send(:binding) in the
#initialize method, but I haven't figured out how yet:
Kernel.eval('self', self.send(:binding)).class.name
=> MyException # *not* 'Foo'
Which makes sense, since MyException hasn't been fully instantiated yet
and self.send(:binding) refers to the current context rather than the
caller's.
So how can I access the caller's context/binding?
No comments:
Post a Comment