Class: Contracts::Builtin::RespondTo

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 variable number of method names as symbols. The contract passes if the argument responds to all of those methods. Example: RespondTo[:password, :credit_card]

Instance Method Summary collapse

Methods inherited from CallableClass

[]

Constructor Details

#initialize(*meths) ⇒ RespondTo

Returns a new instance of RespondTo.



166
167
168
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 166

def initialize(*meths)
  @meths = meths
end

Instance Method Details

#to_sObject



176
177
178
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 176

def to_s
  "a value that responds to #{@meths.inspect}"
end

#valid?(val) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
# File 'lib/contracts-ruby2/lib/contracts/builtin_contracts.rb', line 170

def valid?(val)
  @meths.all? do |meth|
    val.respond_to? meth
  end
end