Often, we can use p foo
or foo.inspect
to see the instance variables, but is it only the default behavior and the object can choose to show somethi
In Ruby, all access protection can be circumvented using reflection:
@bar.instance_variable_get(:@wah)
Trying to print a variable defined by attr_writer from outside the class will throw an error (undefined method 'wah' for #<Bar:0x0000...>
) - but for debugging purposes you can use instance_variable_get as such:
b = Bar.new(:wah => "Hello")
b.wah # undefined method
b.instance_variable_get("@wah") # => "Hello"