Class: Contracts::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.


191
192
193
# File 'lib/contracts/builtin_contracts.rb', line 191

def initialize(cls)
  @cls = cls
end

Instance Method Details

#to_sObject


199
200
201
# File 'lib/contracts/builtin_contracts.rb', line 199

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)

195
196
197
# File 'lib/contracts/builtin_contracts.rb', line 195

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