class Contracts::Builtin::CollectionOf
@private Takes a collection(responds to :each) type and a contract. The related argument must be of specified collection type. Checks the contract against every element of the collection. If it passes for all elements, the contract passes. Example: CollectionOf[Array, Num]
Public Class Methods
new(collection_class, contract)
click to toggle source
Calls superclass method
# File lib/contracts/builtin_contracts.rb, line 294 def initialize(collection_class, contract) super() @collection_class = collection_class @contract = contract end
Public Instance Methods
to_s()
click to toggle source
# File lib/contracts/builtin_contracts.rb, line 309 def to_s "a collection #{@collection_class} of #{@contract}" end
valid?(vals)
click to toggle source
# File lib/contracts/builtin_contracts.rb, line 300 def valid?(vals) return false unless vals.is_a?(@collection_class) vals.all? do |val| res, _ = Contract.valid?(val, @contract) res end end