Created
August 31, 2021 13:26
-
-
Save lulalala/623c38bcc2836e52cfc6da8f4110604b to your computer and use it in GitHub Desktop.
friendly inspect which omits long strings or complex objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def inspect | |
result = instance_variables.each | |
.with_object({}) { |name, h| h[name] = instance_variable_get(name) } | |
.reject { |name, value| value.nil? } | |
.map { |name, value| | |
case value | |
when true, false, Symbol, Numeric, Array, Hash | |
"#{name}=#{value.inspect}" | |
when String | |
if value.length > 80 | |
"#{name}=(omitted)" | |
else | |
"#{name}=#{value.inspect}" | |
end | |
else | |
"#{name}=#<#{value.class.name}>" | |
end | |
} | |
.join(' ') | |
"#<#{self.class.name} " + result + '>' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment