@@ -295,6 +295,17 @@ def sub(pattern, *rest, &block)
295295 self . class . new ( path )
296296 end
297297
298+ # Return a pathname with +repl+ added as a suffix to the basename.
299+ #
300+ # If self has no extension part, +repl+ is appended.
301+ #
302+ # Pathname.new('/usr/bin/shutdown').sub_ext('.rb')
303+ # #=> #<Pathname:/usr/bin/shutdown.rb>
304+ def sub_ext ( repl )
305+ ext = File . extname ( @path )
306+ self . class . new ( @path . chomp ( ext ) + repl )
307+ end
308+
298309 if File ::ALT_SEPARATOR
299310 SEPARATOR_LIST = "#{ Regexp . quote File ::ALT_SEPARATOR } #{ Regexp . quote File ::SEPARATOR } "
300311 SEPARATOR_PAT = /[#{ SEPARATOR_LIST } ]/
@@ -842,6 +853,14 @@ def readlines(...) IO.readlines(@path, ...) end
842853
843854 # See <tt>IO.sysopen</tt>.
844855 def sysopen ( *args ) IO . sysopen ( @path , *args ) end
856+
857+ # Writes +contents+ to the file. See <tt>File.write</tt>.
858+ def write ( ...) IO . write ( @path , ...) end
859+
860+ # Writes +contents+ to the file, opening it in binary mode.
861+ #
862+ # See File.binwrite.
863+ def binwrite ( ...) IO . binwrite ( @path , ...) end
845864end
846865
847866
@@ -850,6 +869,12 @@ class Pathname # * File *
850869 # See <tt>File.atime</tt>. Returns last access time.
851870 def atime ( ) File . atime ( @path ) end
852871
872+ # Returns the birth time for the file.
873+ # If the platform doesn't have birthtime, raises NotImplementedError.
874+ #
875+ # See File.birthtime.
876+ def birthtime ( ) File . birthtime ( @path ) end
877+
853878 # See <tt>File.ctime</tt>. Returns last (directory entry, not file) change time.
854879 def ctime ( ) File . ctime ( @path ) end
855880
@@ -927,6 +952,13 @@ def split()
927952 raise TypeError , 'wrong argument type nil (expected Array)' unless Array === array
928953 array . map { |f | self . class . new ( f ) }
929954 end
955+
956+ # Returns the real (absolute) pathname for +self+ in the actual filesystem.
957+ #
958+ # Does not contain symlinks or useless dots, +..+ and +.+.
959+ #
960+ # All components of the pathname must exist when this method is called.
961+ def realpath ( *args ) self . class . new ( File . realpath ( @path , *args ) ) end
930962end
931963
932964
@@ -938,6 +970,17 @@ def blockdev?() FileTest.blockdev?(@path) end
938970 # See <tt>FileTest.chardev?</tt>.
939971 def chardev? ( ) FileTest . chardev? ( @path ) end
940972
973+ # Tests the file is empty.
974+ #
975+ # See Dir#empty? and FileTest.empty?.
976+ def empty?
977+ if FileTest . directory? ( @path )
978+ Dir . empty? ( @path )
979+ else
980+ FileTest . empty? ( @path )
981+ end
982+ end
983+
941984 # See <tt>FileTest.executable?</tt>.
942985 def executable? ( ) FileTest . executable? ( @path ) end
943986
@@ -1016,6 +1059,21 @@ def Pathname.glob(*args, **kwargs) # :yield: pathname
10161059 end
10171060 end
10181061
1062+ # Returns or yields Pathname objects.
1063+ #
1064+ # Pathname("ruby-2.4.2").glob("R*.md")
1065+ # #=> [#<Pathname:ruby-2.4.2/README.md>, #<Pathname:ruby-2.4.2/README.ja.md>]
1066+ #
1067+ # See Dir.glob.
1068+ # This method uses the +base+ keyword argument of Dir.glob.
1069+ def glob ( *args , **kwargs ) # :yield: pathname
1070+ if block_given?
1071+ Dir . glob ( *args , **kwargs , base : @path ) { |f | yield self + f }
1072+ else
1073+ Dir . glob ( *args , **kwargs , base : @path ) . map { |f | self + f }
1074+ end
1075+ end
1076+
10191077 # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
10201078 def Pathname . getwd ( ) self . new ( Dir . getwd ) end
10211079 class << self ; alias pwd getwd end
0 commit comments