Andrew Grimm
Andrew Grimm

Reputation: 81520

Is there an equivalent of to_yaml_properties for inspect in Ruby?

Object#to_yaml_properties is a method you can use to list which instance variables you want serialized to YAML without having to re-implement the entire serialization process. If you want to exclude certain instance variables, you could use super and then filter out the ones you don't want.

Is there an equivalent method that allows you to only list certain variables when calling Object#inspect, or would I have to re-implement the entire method?

Upvotes: 1

Views: 300

Answers (1)

Phrogz
Phrogz

Reputation: 303253

There is no such mechanism in vanilla Ruby.

On the plus side, you could implement it and then use it in your redefinition of MyClass#inspect. You don't have to do all the work per class, just create a class-level method that allows you to set the inspect.

Or you could add your own Object#limited_inspect and redefine Kernel#p (or add your own shortcut) to use all instance variables minus whatever ones might be excluded.

Upvotes: 1

Related Questions