On Wed, 4 Oct 2006, Trans wrote: > I feel like I should know how to do this already, but... How do I get > the context in which an error was thrown? > > x = SomeClass > > def dosomething > x.bar > end > > begin > dosomething > rescue NoMethodError > p e.context #=> SomeClass (HOW?) > end > > Thanks, > T. but, imho, that's not the context. the context would be 'dosomething' ? can you explain what you mean by 'context'? if by context you mean the object raising the NoMethodError, you can do obviously hook into method_missing to make it add this info to every execption, otherwise this is all you've got: harp:~ > cat a.rb class C; end begin C.foobar rescue NoMethodError => ne (NoMethodError.instance_methods - Object.instance_methods).sort.each do |m| p m => ne.send(m) end end harp:~ > ruby a.rb {"args"=>[]} {"backtrace"=>["a.rb:4"]} {"exception"=>#<NoMethodError: undefined method `foobar' for C:Class>} {"message"=>"undefined method `foobar' for C:Class"} {"name"=>:foobar} a.rb:7:in `set_backtrace': wrong number of arguments (0 for 1) (ArgumentError) from a.rb:7 from a.rb:6 so, other than scraping that info - which does contain what you want - i think adding an attr to Exception and making method_missing set it on the way out might be the only way to go... cheers. -a -- in order to be effective truth must penetrate like an arrow - and that is likely to hurt. -- wei wu wei