Skip to content

Commit 8557cd2

Browse files
nobubyroot
authored andcommitted
[Feature #21219] Selective inspect of instance variables
Make Kernel#inspect ask which instance variables should be dumped by the result of `#instance_variables_to_inspect`. Co-Authored-By: Jean Boussier <[email protected]>
1 parent d900403 commit 8557cd2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

core/kernel/inspect_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,34 @@ class << obj
2828
end
2929
obj.inspect.should be_kind_of(String)
3030
end
31+
32+
ruby_version_is "3.5" do
33+
it "calls #instance_variables_to_inspect private method to know which variables to display" do
34+
obj = Object.new
35+
obj.instance_eval do
36+
@host = "localhost"
37+
@user = "root"
38+
@password = "hunter2"
39+
end
40+
obj.singleton_class.class_eval do
41+
private def instance_variables_to_inspect = %i[@host @user @does_not_exist]
42+
end
43+
44+
inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00')
45+
inspected.should == '#<Object:0x00 @host="localhost", @user="root">'
46+
47+
obj = Object.new
48+
obj.instance_eval do
49+
@host = "localhost"
50+
@user = "root"
51+
@password = "hunter2"
52+
end
53+
obj.singleton_class.class_eval do
54+
private def instance_variables_to_inspect = []
55+
end
56+
57+
inspected = obj.inspect.sub(/^#<Object:0x[0-9a-f]+/, '#<Object:0x00')
58+
inspected.should == "#<Object:0x00>"
59+
end
60+
end
3161
end

0 commit comments

Comments
 (0)