Class: Contracts::Builtin::RangeOf

Inherits:
CallableClass show all
Defined in:
lib/contracts-ruby2/lib/contracts/builtin_contracts.rb,
lib/contracts-ruby3/lib/contracts/builtin_contracts.rb

Overview

Use this to specify a Range object of a particular datatype. Example: RangeOf[Nat], RangeOf[Date], …

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(contract) ⇒ RangeOf

Returns a new instance of RangeOf.



345
346
347
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 345

def initialize(contract)
  @contract = contract
end

Instance Method Details

#to_sObject



355
356
357
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 355

def to_s
  "a range of #{@contract}"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


349
350
351
352
353
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 349

def valid?(val)
  val.is_a?(Range) &&
    Contract.valid?(val.first, @contract) &&
    Contract.valid?(val.last,  @contract)
end