class RBS::TypeName
TypeName represents name of types in RBS.
TypeNames are one of the three kind, class, alias, and interface. class type names corresponds to Ruby classes and modules. There are no corresponding Ruby value to alias and interface type names.
Attributes
kind
Kind of the type.
The namespace the type name is defined in.
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 36 def self.[](namespace, name) ns = Namespace[namespace.path, namespace.absolute?] inner = @intern_cache[ns] if inner && (cached = inner[name]) return cached end @intern_mutex.synchronize do inner = (@intern_cache[ns] ||= {}) inner[name] ||= new(namespace: ns, name: name) end end
Returns a canonical TypeName instance for the given namespace / name pair. The namespace is canonicalized through Namespace.[] so identity-based lookup works regardless of the caller passing a fresh Namespace.new or an already-interned instance.
Returns an interned TypeName instance.
The given namespace is canonicalized so cached instances always hold an interned namespace. Subsequent calls with structurally equal arguments return the same object.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 9 def initialize(namespace:, name:) @namespace = namespace @name = name @kind = case when name.match?(/\A[A-Z]/) :class when name.match?(/\A[a-z]/) :alias when name.start_with?("_") :interface else # Defaults to :class :class end end
Initializer accepts two keyword args, namespace and name. Note that kind is automatically determined from its name.
If the name starts with capital alphabet, it is class. If the name starts with lower case alphabet, it is alias. If the name starts with an underscore, it is interface.
(String name) → RBS::TypeName
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 113 def self.parse(string) absolute = string.start_with?("::") *path, name = string.delete_prefix("::").split("::").map(&:to_sym) raise unless name TypeName[Namespace[path, absolute], name] end
Returns type name with given string representation.
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 105 def +(other) if other.absolute? other else TypeName[self.to_namespace + other.namespace, other.name] end end
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 50 def ==(other) return true if equal?(other) other.is_a?(self.class) && other.namespace == namespace && other.name == name end
() → TypeName
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 81 def absolute! TypeName[namespace.absolute!, name] end
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 85 def absolute? namespace.absolute? end
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 77 def alias? kind == :alias end
Returns true when self is an alias type name.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 73 def class? kind == :class end
Returns true when self is a class type name.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 57 def hash @hash ||= namespace.hash ^ name.hash end
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 93 def interface? kind == :interface end
Returns true when self is an interface type name.
() → TypeName
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 89 def relative! TypeName[namespace.relative!, name] end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 101 def split namespace.path + [name] end
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 65 def to_json(state = nil) to_s.to_json(state) end
() → Namespace
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 69 def to_namespace namespace.append(self.name) end
Returns a namespace with same components of self.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 61 def to_s "#{namespace.to_s}#{name}" end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/type_name.rb, line 97 def with_prefix(namespace) TypeName[namespace + self.namespace, name] end
Returns a new type name with a namespace appended to given namespace.
TypeName.parse("Hello").with_prefix(Namespace("World")) # => World::Hello TypeName.parse("Foo::Bar").with_prefix(Namespace("::Hello")) # => ::Hello::Foo::Bar TypeName.parse("::A::B").with_prefix(Namespace("C")) # => ::A::B