@@ -277,6 +277,17 @@ def sub(pattern, *rest, &block)
277277 self . class . new ( path )
278278 end
279279
280+ # Return a pathname with +repl+ added as a suffix to the basename.
281+ #
282+ # If self has no extension part, +repl+ is appended.
283+ #
284+ # Pathname.new('/usr/bin/shutdown').sub_ext('.rb')
285+ # #=> #<Pathname:/usr/bin/shutdown.rb>
286+ def sub_ext ( repl )
287+ ext = File . extname ( @path )
288+ self . class . new ( @path . chomp ( ext ) + repl )
289+ end
290+
280291 if File ::ALT_SEPARATOR
281292 SEPARATOR_LIST = "#{ Regexp . quote File ::ALT_SEPARATOR } #{ Regexp . quote File ::SEPARATOR } "
282293 SEPARATOR_PAT = /[#{ SEPARATOR_LIST } ]/
@@ -824,6 +835,14 @@ def readlines(...) IO.readlines(@path, ...) end
824835
825836 # See <tt>IO.sysopen</tt>.
826837 def sysopen ( *args ) IO . sysopen ( @path , *args ) end
838+
839+ # Writes +contents+ to the file. See <tt>File.write</tt>.
840+ def write ( ...) IO . write ( @path , ...) end
841+
842+ # Writes +contents+ to the file, opening it in binary mode.
843+ #
844+ # See File.binwrite.
845+ def binwrite ( ...) IO . binwrite ( @path , ...) end
827846end
828847
829848
@@ -832,6 +851,12 @@ class Pathname # * File *
832851 # See <tt>File.atime</tt>. Returns last access time.
833852 def atime ( ) File . atime ( @path ) end
834853
854+ # Returns the birth time for the file.
855+ # If the platform doesn't have birthtime, raises NotImplementedError.
856+ #
857+ # See File.birthtime.
858+ def birthtime ( ) File . birthtime ( @path ) end
859+
835860 # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
836861 def ctime ( ) File . ctime ( @path ) end
837862
@@ -909,6 +934,13 @@ def split()
909934 raise TypeError , 'wrong argument type nil (expected Array)' unless Array === array
910935 array . map { |f | self . class . new ( f ) }
911936 end
937+
938+ # Returns the real (absolute) pathname for +self+ in the actual filesystem.
939+ #
940+ # Does not contain symlinks or useless dots, +..+ and +.+.
941+ #
942+ # All components of the pathname must exist when this method is called.
943+ def realpath ( *args ) self . class . new ( File . realpath ( @path , *args ) ) end
912944end
913945
914946
@@ -920,6 +952,17 @@ def blockdev?() FileTest.blockdev?(@path) end
920952 # See <tt>FileTest.chardev?</tt>.
921953 def chardev? ( ) FileTest . chardev? ( @path ) end
922954
955+ # Tests the file is empty.
956+ #
957+ # See Dir#empty? and FileTest.empty?.
958+ def empty?
959+ if FileTest . directory? ( @path )
960+ Dir . empty? ( @path )
961+ else
962+ FileTest . empty? ( @path )
963+ end
964+ end
965+
923966 # See <tt>FileTest.executable?</tt>.
924967 def executable? ( ) FileTest . executable? ( @path ) end
925968
@@ -998,6 +1041,21 @@ def Pathname.glob(*args, **kwargs) # :yield: pathname
9981041 end
9991042 end
10001043
1044+ # Returns or yields Pathname objects.
1045+ #
1046+ # Pathname("ruby-2.4.2").glob("R*.md")
1047+ # #=> [#<Pathname:ruby-2.4.2/README.md>, #<Pathname:ruby-2.4.2/README.ja.md>]
1048+ #
1049+ # See Dir.glob.
1050+ # This method uses the +base+ keyword argument of Dir.glob.
1051+ def glob ( *args , **kwargs ) # :yield: pathname
1052+ if block_given?
1053+ Dir . glob ( *args , **kwargs , base : @path ) { |f | yield self + f }
1054+ else
1055+ Dir . glob ( *args , **kwargs , base : @path ) . map { |f | self + f }
1056+ end
1057+ end
1058+
10011059 # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
10021060 def Pathname . getwd ( ) self . new ( Dir . getwd ) end
10031061 class << self ; alias pwd getwd end
0 commit comments