lib/rbs/namespace.rb in rbs-2.6.0 vs lib/rbs/namespace.rb in rbs-2.7.0.pre.1

- old
+ new

@@ -1,20 +1,22 @@ +# frozen_string_literal: true + module RBS class Namespace attr_reader :path def initialize(path:, absolute:) @path = path - @absolute = absolute + @absolute = absolute ? true : false end def self.empty - new(path: [], absolute: false) + @empty ||= new(path: [], absolute: false) end def self.root - new(path: [], absolute: true) + @root ||= new(path: [], absolute: true) end def +(other) if other.absolute? other @@ -26,11 +28,13 @@ def append(component) self.class.new(path: path + [component], absolute: absolute?) end def parent - raise "Parent with empty namespace" if empty? - self.class.new(path: path.take(path.size - 1), absolute: absolute?) + @parent ||= begin + raise "Parent with empty namespace" if empty? + self.class.new(path: path.take(path.size - 1), absolute: absolute?) + end end def absolute? @absolute end