Class: Types::Method

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/method.rb

Overview

Represents a method type attached to a receiver type.

“‘ruby type = Types::Method(Types::String, Types::Integer, returns: Types::String) type.to_s # => “Method(String, Integer, returns: String)” “`

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generic

#|

Constructor Details

#initialize(receiver_type, argument_types, return_type) ⇒ Method

Returns a new instance of Method.



22
23
24
25
26
# File 'lib/types/method.rb', line 22

def initialize(receiver_type, argument_types, return_type)
	@receiver_type = receiver_type
	@argument_types = argument_types
	@return_type = return_type
end

Instance Attribute Details

#argument_typesObject (readonly)

Returns the value of attribute argument_types.



31
32
33
# File 'lib/types/method.rb', line 31

def argument_types
  @argument_types
end

#receiver_typeObject (readonly)

Returns the value of attribute receiver_type.



29
30
31
# File 'lib/types/method.rb', line 29

def receiver_type
  @receiver_type
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



33
34
35
# File 'lib/types/method.rb', line 33

def return_type
  @return_type
end

Instance Method Details

#composite?Boolean

Returns:



36
37
38
# File 'lib/types/method.rb', line 36

def composite?
	true
end

#parse(input) ⇒ Object

Parses the input as a method or proc.



68
69
70
71
72
73
74
75
76
77
# File 'lib/types/method.rb', line 68

def parse(input)
	case input
	when ::String
		receiver_type.resolve.instance_method(input)
	when ::Proc
		input
	else
		raise ArgumentError, "Cannot coerce #{input.inspect} into Method!"
	end
end

#to_rbsObject



60
61
62
# File 'lib/types/method.rb', line 60

def to_rbs
	"Method"
end

#to_sObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/types/method.rb', line 41

def to_s
	buffer = ::String.new
	
	if @argument_types&.any?
		buffer << "Method(#{@receiver_type}, #{@argument_types.join(', ')}"
	else
		buffer << "Method(#{@receiver_type}, "
	end
	
	if @return_type
		buffer << "returns: #{@return_type})"
	else
		buffer << ")"
	end
	
	return buffer
end