Class: Contracts::Builtin::Xor

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

Overview

Takes a variable number of contracts. The contract passes if exactly one of those contracts pass. Example: Xor[Fixnum, Float]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*vals) ⇒ Xor

Returns a new instance of Xor.



123
124
125
# File 'lib/contracts/builtin_contracts.rb', line 123

def initialize(*vals)
  @vals = vals
end

Instance Method Details

#to_sObject



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

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/contracts/builtin_contracts.rb', line 127

def valid?(val)
  results = @vals.map do |contract|
    res,  = Contract.valid?(val, contract)
    res
  end
  results.count(true) == 1
end