Class: Contracts::Builtin::Exactly

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

Overview

Takes a class A. If argument is object of type A, the contract passes. If it is a subclass of A (or not related to A in any way), it fails. Example: Exactly[Numeric]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(cls) ⇒ Exactly

Returns a new instance of Exactly.



205
206
207
# File 'lib/contracts/builtin_contracts.rb', line 205

def initialize(cls)
  @cls = cls
end

Instance Method Details

#to_sObject



213
214
215
# File 'lib/contracts/builtin_contracts.rb', line 213

def to_s
  "exactly #{@cls.inspect}"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/contracts/builtin_contracts.rb', line 209

def valid?(val)
  val.class == @cls
end