class RBS::Namespace
Namespace instance represents a prefix of module names.
vvvvvvvvvvvvvv TypeName RBS::Namespace ^^^^^ Namespace vvvvvvvvvv TypeName RBS::Types ^^^^^ Namespace vvvvvvvvvvvvvvvvv TypeName RBS::Types::Union ^^^^^^^^^^^^ Namespace
Note that Namespace is an RBS specific concept and there is no corresponding concept in Ruby.
There are absolute and relative namespaces.
Namespace(::RBS::) # Absolute namespace Namespace( RBS::) # Relative namespace
It also defines two special namespaces.
:: # _Root_ namespace
# _Empty_ namespace
Constants
- INTERN_LEAF
Attributes
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 24 def self.[](path, absolute) absolute = absolute ? true : false # Lock-free fast path. node = absolute ? @intern_trie_absolute : @intern_trie_relative path.each do |sym| node = node[sym] break unless node end if node && (cached = node[INTERN_LEAF]) return cached end @intern_mutex.synchronize do node = absolute ? @intern_trie_absolute : @intern_trie_relative path.each { |sym| node = (node[sym] ||= {}) } node[INTERN_LEAF] ||= begin frozen_path = path.frozen? ? path : path.dup.freeze new(path: frozen_path, absolute: absolute) end end end
Returns a canonical Namespace instance for the given path / absolute pair. Repeated calls with structurally equal arguments return the same object, so callers can rely on equal? for fast equality. The path Array is duplicated and frozen on insert.
Returns an interned Namespace instance.
Subsequent calls with structurally equal path and absolute arguments return the same object. The internal path array is duplicated and frozen before being cached.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 47 def self.empty @empty ||= self[[], false] end
Returns new empty namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 7 def initialize(path:, absolute:) @path = path @absolute = absolute ? true : false end
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 55 def +(other) if other.absolute? other else Namespace[path + other.path, absolute?] end end
Concat two namespaces.
Namespace("Foo::") + Namespace("Bar::") # => Foo::Bar:: Namespace("::Foo::") + Namespace("Bar::") # => ::Foo::Bar::
If other is an absolute namespace, it returns other.
Namespace("Foo::") + Namespace("::Bar::") # => ::Bar::
(untyped other) → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 94 def ==(other) return true if equal?(other) other.is_a?(Namespace) && other.path == path && other.absolute? == absolute? end
Equality is defined by its structure.
() → Namespace
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 82 def absolute! Namespace[path, true] end
Returns absolute namespace.
Namespace("A").absolute! # => Namespace("::A") Namespace("::A").absolute! # => Namespace("::A")
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 74 def absolute? @absolute end
Returns true if self is absolute namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 63 def append(component) Namespace[path + [component], absolute?] end
Add one path component to self.
Namespace("Foo::").append(:Bar) # => Namespace("Foo::Bar::")
() { (Namespace) → void } → void
() → Enumerator[Namespace, void]
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 137 def ascend if block_given? current = self until current.empty? yield current current = _ = current.parent end yield current self else enum_for(:ascend) end end
Iterate over Namespace for each element in ascending order.
Namespace.parse("::A::B::C").ascend {|ns| p ns } # => ::A::B::C # => ::A::B # => ::A # => ::(root)
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 90 def empty? path.empty? end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 101 def hash @hash ||= path.hash ^ absolute?.hash end
Hash is defined based on its structure.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 67 def parent @parent ||= begin raise "Parent with empty namespace" if empty? Namespace[path.take(path.size - 1), absolute?] end end
Returns parent namespace. Raises error there is no parent namespace.
Namespace("::A").parent # => Namespace("::") Namespace("::").parent # raises error Namespace("A::B").parent # => Namespace("A")
() → Namespace
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 86 def relative! Namespace[path, false] end
Returns relative namespace.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 78 def relative? !absolute? end
Returns true if self is relative namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 105 def split last = path.last or return parent = self.parent [parent, last] end
Returns a pair of parent namespace and a symbol of last component.
Namespace("::A::B::C").split # => [Namespace("::A::B::"), :C]
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 111 def to_s if empty? absolute? ? "::" : "" else s = path.join("::") absolute? ? "::#{s}::" : "#{s}::" end end
() → TypeName
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.1.3/lib/rbs/namespace.rb, line 120 def to_type_name parent, name = split raise unless name raise unless parent TypeName[parent, name] end
Construct a type name which points to the same name type.