Class: Contracts::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[Fixnum, Float]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ And

Returns a new instance of And.


128
129
130
# File 'lib/contracts/builtin_contracts.rb', line 128

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject


139
140
141
142
143
# File 'lib/contracts/builtin_contracts.rb', line 139

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)

132
133
134
135
136
137
# File 'lib/contracts/builtin_contracts.rb', line 132

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