Class: Contracts::Builtin::KeywordArgs

Inherits:
CallableClass show all
Defined in:
lib/contracts-ruby2/lib/contracts/builtin_contracts.rb,
lib/contracts-ruby3/lib/contracts/builtin_contracts.rb

Overview

Use this for specifying contracts for keyword arguments Example: KeywordArgs[ e: Range, f: Optional[Num] ]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(options) ⇒ KeywordArgs

Returns a new instance of KeywordArgs.



419
420
421
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 419

def initialize(options)
  @options = options
end

Instance Method Details

#inspectObject



435
436
437
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 435

def inspect
  to_s
end

#to_sObject



431
432
433
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 431

def to_s
  "KeywordArgs[#{options}]"
end

#valid?(hash) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
426
427
428
429
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 423

def valid?(hash)
  return false unless hash.is_a?(Hash)
  return false unless hash.keys - options.keys == []
  options.all? do |key, contract|
    Optional._valid?(hash, key, contract)
  end
end