Method: Hash#has_shape?
- Defined in:
- lib/shenanigans/hash/has_shape_pred.rb
#has_shape?(shape) ⇒ Boolean
Checks if a hash has a certain structure.
h = { k1: 1, k2: "1" }
h.has_shape?(k1: Fixnum, k2: String)
#=> true
h.has_shape?(k1: Class, k2: String)
#=> false
It also works with compound data structures.
h = { k1: [], k2: { k3: Struct.new("Foo") } }
shape = { k1: Array, k2: { k3: Module } }
h.has_shape?(shape)
#=> true
13 14 15 16 17 |
# File 'lib/shenanigans/hash/has_shape_pred.rb', line 13 def has_shape?(shape) all? do |k, v| Hash === v ? v.has_shape?(shape[k]) : shape[k] === v end end |