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]
Constant Summary collapse
- INVALID_KEY_VALUE_PAIR =
"You should provide only one key-value pair to HashOf contract"
Instance Method Summary collapse
-
#initialize(key, value = nil) ⇒ HashOf
constructor
A new instance of HashOf.
- #to_s ⇒ Object
- #valid?(hash) ⇒ Boolean
Methods inherited from CallableClass
Constructor Details
#initialize(key, value = nil) ⇒ HashOf
Returns a new instance of HashOf.
337 338 339 340 341 342 343 344 345 346 |
# File 'lib/contracts/builtin_contracts.rb', line 337 def initialize(key, value = nil) if value @key = key @value = value else validate_hash(key) @key = key.keys.first @value = key[@key] end end |
Instance Method Details
#to_s ⇒ Object
355 356 357 |
# File 'lib/contracts/builtin_contracts.rb', line 355 def to_s "Hash<#{@key}, #{@value}>" end |