lib/fakefs/pathname.rb
def sub(pattern, *rest, &block)
      path =
        if block
          @path.sub(pattern, *rest) do |*args|
            begin
              old = Thread.current[:pathname_sub_matchdata]
              Thread.current[:pathname_sub_matchdata] = $~
              # TODO: rewrite without using eval
              eval(
                '$~ = Thread.current[:pathname_sub_matchdata]',
                block.binding,
                __FILE__,
                __LINE__ - 3
              )
            ensure
              Thread.current[:pathname_sub_matchdata] = old
            end
            yield(*args)
          end
        else
          @path.sub(pattern, *rest)
        end
      self.class.new(path)
    end
def sub(pattern, *rest, &block)
      path =
        if block
          @path.sub(pattern, *rest) do |*args|
            begin
              old = Thread.current[:pathname_sub_matchdata]
              Thread.current[:pathname_sub_matchdata] = $~
              # TODO: rewrite without using eval
              eval(
                '$~ = Thread.current[:pathname_sub_matchdata]',
                block.binding,
                __FILE__,
                __LINE__ - 3
              )
            ensure
              Thread.current[:pathname_sub_matchdata] = old
            end
            yield(*args)
          end
        else
          @path.sub(pattern, *rest)
        end
      self.class.new(path)
    end
Return a pathname which is substituted by String#sub.