Class: Contracts::Builtin::Exactly

Inherits:
CallableClass show all
Defined in:
lib/contracts-ruby2/lib/contracts/builtin_contracts.rb,
lib/contracts-ruby3/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.



206
207
208
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 206

def initialize(cls)
  @cls = cls
end

Instance Method Details

#to_sObject



214
215
216
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 214

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

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 210

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