Class: Contracts::Builtin::And

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

Overview

Takes a variable number of contracts. The contract passes if all contracts pass. Example: And[Integer, Float]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ And

Returns a new instance of And.



151
152
153
154
# File 'lib/contracts/builtin_contracts.rb', line 151

def initialize(*vals)
  super()
  @vals = vals
end

Instance Method Details

#to_sObject



163
164
165
166
167
168
169
# File 'lib/contracts/builtin_contracts.rb', line 163

def to_s
  # rubocop:disable Style/StringConcatenation
  @vals[0, @vals.size-1].map do |x|
    InspectWrapper.create(x)
  end.join(", ") + " and " + InspectWrapper.create(@vals[-1]).to_s
  # rubocop:enable Style/StringConcatenation
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
# File 'lib/contracts/builtin_contracts.rb', line 156

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