Class: Contracts::Builtin::Or

Inherits:
CallableClass show all
Defined in:
lib/contracts/builtin_contracts.rb

Overview

Takes a variable number of contracts. The contract passes if any of the contracts pass. Example: Or[Fixnum, Float]

Direct Known Subclasses

Maybe

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ Or

Returns a new instance of Or.



103
104
105
# File 'lib/contracts/builtin_contracts.rb', line 103

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject



114
115
116
# File 'lib/contracts/builtin_contracts.rb', line 114

def to_s
  EnumInspector.inspect(@vals, "or")
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
# File 'lib/contracts/builtin_contracts.rb', line 107

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