When an exception in thrown for a ruby object, the object's properties are serialized into the exception message.
class MyClass def initialize @hash = { user: 'user_name', password: 'value_that_should_not_be_dumped' } endendbegin MyClass.new.foorescue StandardError => e puts e.messageend
Sample Output
bash-4.2$ bundle exec ruby exc.rbundefined method `foo' for #<MyClass:0x00007feae939b378 @hash={:user=>"user_name", :password=>"value_that_should_not_be_dumped"}>
I am able to prevent this if I override the Object.inspect
method.
class Object def inspect"#{self.class} (values suppressed)" endend
Sample Output
bash-4.2$ bundle exec ruby exc.rbundefined method `foo' for MyClass (values suppressed):MyClass
Ideally, I would like to indicate whether or not StandardError should invoke inspect
on the object in question. Alternatively, I would like to be able to mark specific properties for exclusion from the inspect operation.
Is there a good way to accomplish this in ruby?