Class: Contracts::HashOf
- Inherits:
-
CallableClass
- Object
- CallableClass
- Contracts::HashOf
- Defined in:
- lib/contracts/builtin_contracts.rb
Overview
Use this to specify the Hash characteristics. Takes two contracts, one for hash keys and one for hash values. Example: HashOf[Symbol, String]
Instance Method Summary collapse
-
#initialize(key, value) ⇒ HashOf
constructor
A new instance of HashOf.
- #to_s ⇒ Object
- #valid?(hash) ⇒ Boolean
Methods inherited from CallableClass
Constructor Details
#initialize(key, value) ⇒ HashOf
Returns a new instance of HashOf.
369 370 371 372 |
# File 'lib/contracts/builtin_contracts.rb', line 369 def initialize(key, value) @key = key @value = value end |
Instance Method Details
#to_s ⇒ Object
381 382 383 |
# File 'lib/contracts/builtin_contracts.rb', line 381 def to_s "Hash<#{@key}, #{@value}>" end |
#valid?(hash) ⇒ Boolean
374 375 376 377 378 379 |
# File 'lib/contracts/builtin_contracts.rb', line 374 def valid?(hash) keys_match = hash.keys.map { |k| Contract.valid?(k, @key) }.all? vals_match = hash.values.map { |v| Contract.valid?(v, @value) }.all? [keys_match, vals_match].all? end |