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.


219
220
221
222
# File 'lib/contracts/builtin_contracts.rb', line 219

def initialize(cls)
  super()
  @cls = cls
end

Instance Method Details

#to_sObject


228
229
230
# File 'lib/contracts/builtin_contracts.rb', line 228

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)

224
225
226
# File 'lib/contracts/builtin_contracts.rb', line 224

def valid?(val)
  val.instance_of?(@cls)
end