In Ruby, when I do something like this:
class Foo
...
def initialize( var )
@var = var
end
...
end
Then if I return a foo in console I get this object representation:
#<Foo:0x12345678910234 @var=...........>
Sometimes I have an instance variable that is a long hash or something and it makes reading the rest of the object much more difficult.
My question is: is there a way to set an instance variable in an object to “private” or otherwise invisible so that it won’t be printed as part of the object representation if that object is returned in the console?
Thanks!
After some quick searching, I don’t think Ruby supports private instance variables. Your best bet is to override your object’s
to_smethod (or monkey patchObject#to_s) to only output the instance variables you want to see. To make things easier, you could create a blacklist of variables you want to hide:Note that they will still be accessible through
obj.instance_variablesandobj.instance_variable_get, but at the very least they won’t get in the way of your debugging.