Class: Types::Hash
Overview
Represents a hash type with key and value types.
“‘ruby type = Types::Hash.new(Types::String, Types::Integer) type.parse(=> “42”) # => => 42 “`
Instance Method Summary collapse
- #composite? ⇒ Boolean
-
#initialize(key_type, value_type) ⇒ Hash
constructor
A new instance of Hash.
-
#parse(input) ⇒ Object
Parses the input as a hash with the specified key and value types.
-
#resolve ⇒ Object
Resolves to the actual Ruby Hash class.
- #to_rbs ⇒ Object
- #to_s ⇒ Object
Methods included from Generic
Constructor Details
#initialize(key_type, value_type) ⇒ Hash
Returns a new instance of Hash.
21 22 23 24 |
# File 'lib/types/hash.rb', line 21 def initialize(key_type, value_type) @key_type = key_type @value_type = value_type end |
Instance Method Details
#parse(input) ⇒ Object
Parses the input as a hash with the specified key and value types.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/types/hash.rb', line 35 def parse(input) case input when ::String return parse_values(parse_string(input)) when ::Hash return parse_values(input) else raise ArgumentError, "Cannot coerce #{input.inspect} into Hash!" end end |
#resolve ⇒ Object
Resolves to the actual Ruby Hash class.
58 59 60 |
# File 'lib/types/hash.rb', line 58 def resolve ::Hash end |
#to_rbs ⇒ Object
52 53 54 |
# File 'lib/types/hash.rb', line 52 def to_rbs "Hash[#{@key_type.to_rbs}, #{@value_type.to_rbs}]" end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/types/hash.rb', line 47 def to_s "Hash(#{@key_type}, #{@value_type})" end |