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
Attributes
Public Class Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 12 def self.empty @empty ||= new(path: [], absolute: false) end
Returns new empty namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 7 def initialize(path:, absolute:) @path = path @absolute = absolute ? true : false end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 93 def self.parse(string) if string.start_with?("::") new(path: string.split("::").drop(1).map(&:to_sym), absolute: true) else new(path: string.split("::").map(&:to_sym), absolute: false) end end
Public Instance Methods
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 20 def +(other) if other.absolute? other else self.class.new(path: path + other.path, absolute: 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.0.3/lib/rbs/namespace.rb, line 59 def ==(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.0.3/lib/rbs/namespace.rb, line 47 def absolute! self.class.new(path: path, absolute: 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.0.3/lib/rbs/namespace.rb, line 39 def absolute? @absolute end
Returns true if self is absolute namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 28 def append(component) self.class.new(path: path + [component], absolute: 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.0.3/lib/rbs/namespace.rb, line 101 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.0.3/lib/rbs/namespace.rb, line 55 def empty? path.empty? end
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 65 def hash path.hash ^ absolute?.hash end
Hash is defined based on its structure.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 32 def parent @parent ||= begin raise "Parent with empty namespace" if empty? self.class.new(path: path.take(path.size - 1), absolute: 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.0.3/lib/rbs/namespace.rb, line 51 def relative! self.class.new(path: path, absolute: false) end
Returns relative namespace.
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 43 def relative? !absolute? end
Returns true if self is relative namespace.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/lib/rbs/namespace.rb, line 69 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.0.3/lib/rbs/namespace.rb, line 75 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.0.3/lib/rbs/namespace.rb, line 84 def to_type_name parent, name = split raise unless name raise unless parent TypeName.new(name: name, namespace: parent) end
Construct a type name which points to the same name type.