Class: Contracts::Builtin::Or

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 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.



98
99
100
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 98

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject



109
110
111
112
113
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 109

def to_s
  @vals[0, @vals.size-1].map do |x|
    InspectWrapper.create(x)
  end.join(", ") + " or " + InspectWrapper.create(@vals[-1]).to_s
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 102

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