Class: Contracts::Builtin::Not

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

Overview

Takes a variable number of contracts. The contract passes if all of those contracts fail for the given argument. Example: Not[nil]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ Not

Returns a new instance of Not.



254
255
256
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 254

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject



265
266
267
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 265

def to_s
  "a value that is none of #{@vals.inspect}"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


258
259
260
261
262
263
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 258

def valid?(val)
  @vals.all? do |contract|
    res, _ = Contract.valid?(val, contract)
    !res
  end
end