Class: T::Types::Proc
Overview
Defines the type of a proc (a ruby callable). At runtime, only validates that the value is a ‘::Proc`.
At present, we only support fixed-arity procs with no optional or keyword arguments.
Instance Attribute Summary collapse
-
#arg_types ⇒ Object
readonly
Returns the value of attribute arg_types.
-
#returns ⇒ Object
readonly
Returns the value of attribute returns.
Instance Method Summary collapse
-
#initialize(arg_types, returns) ⇒ Proc
constructor
A new instance of Proc.
- #name ⇒ Object
- #valid?(obj) ⇒ Boolean
Methods inherited from Base
#==, #describe_obj, #error_message_for_obj, #hash, method_added, #subtype_of?, #to_s, #validate!
Constructor Details
#initialize(arg_types, returns) ⇒ Proc
Returns a new instance of Proc.
14 15 16 17 18 19 20 |
# File 'lib/types/types/proc.rb', line 14 def initialize(arg_types, returns) @arg_types = {} arg_types.each do |key, raw_type| @arg_types[key] = T::Utils.coerce(raw_type) end @returns = T::Utils.coerce(returns) end |
Instance Attribute Details
#arg_types ⇒ Object (readonly)
Returns the value of attribute arg_types.
11 12 13 |
# File 'lib/types/types/proc.rb', line 11 def arg_types @arg_types end |
#returns ⇒ Object (readonly)
Returns the value of attribute returns.
12 13 14 |
# File 'lib/types/types/proc.rb', line 12 def returns @returns end |
Instance Method Details
#name ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/types/types/proc.rb', line 23 def name args = [] @arg_types.each do |k, v| args << "#{k}: #{v.name}" end "T.proc.params(#{args.join(', ')}).returns(#{returns})" end |
#valid?(obj) ⇒ Boolean
32 33 34 |
# File 'lib/types/types/proc.rb', line 32 def valid?(obj) obj.is_a?(::Proc) end |