Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4623

Prevent property value inspect when exception is thrown

$
0
0

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?


Viewing all articles
Browse latest Browse all 4623


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>