Skip to content

Instantly share code, notes, and snippets.

@nsmmrs
Created April 22, 2023 04:24
Show Gist options
  • Save nsmmrs/aa908b82a11fba16b6efa3424289b3f6 to your computer and use it in GitHub Desktop.
Save nsmmrs/aa908b82a11fba16b6efa3424289b3f6 to your computer and use it in GitHub Desktop.
Hash#shape
class Hash
def shape
self.class.shape_of self
end
def self.shape_of(hash)
hash.sort_by{|k,v| k.to_s }.map do |k,v|
v = case v
when Array
v.map{|e| shape_of(e) }
when Hash
shape_of(v)
else
v.class
end
[k,v]
end.to_h
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment