Skip to content

Instantly share code, notes, and snippets.

@lulalala
Created August 31, 2021 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lulalala/623c38bcc2836e52cfc6da8f4110604b to your computer and use it in GitHub Desktop.
Save lulalala/623c38bcc2836e52cfc6da8f4110604b to your computer and use it in GitHub Desktop.
friendly inspect which omits long strings or complex objects
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