@@ -146,6 +146,12 @@ module ::Kernel
146146# === File property and manipulation methods
147147#
148148# These methods are a facade for File:
149+ # - #each_line(*args, &block)
150+ # - #read(*args)
151+ # - #binread(*args)
152+ # - #readlines(*args)
153+ # - #write(*args)
154+ # - #binwrite(*args)
149155# - #atime
150156# - #birthtime
151157# - #ctime
@@ -186,14 +192,8 @@ module ::Kernel
186192#
187193# === IO
188194#
189- # These methods are a facade for IO:
190- # - #each_line(*args, &block)
191- # - #read(*args)
192- # - #binread(*args)
193- # - #readlines(*args)
195+ # This method is a facade for IO:
194196# - #sysopen(*args)
195- # - #write(*args)
196- # - #binwrite(*args)
197197#
198198# === Utilities
199199#
@@ -214,6 +214,7 @@ module ::Kernel
214214#
215215class Pathname
216216
217+ # The version string.
217218 VERSION = "0.4.0"
218219
219220 # :stopdoc:
@@ -239,6 +240,9 @@ class Pathname
239240 #
240241 def initialize ( path )
241242 path = path . to_path if path . respond_to? :to_path
243+
244+ raise TypeError unless path . is_a? ( String ) # Compatibility for C version
245+
242246 if path . include? ( "\0 " )
243247 raise ArgumentError , "pathname contains \\ 0: #{ path . inspect } "
244248 end
@@ -338,14 +342,42 @@ def sub_ext(repl)
338342 end
339343
340344 if File . dirname ( 'A:' ) == 'A:.' # DOSish drive letter
341- ABSOLUTE_PATH = /\A (?:[A-Za-z]:|#{ SEPARATOR_PAT } )/o
345+ ABSOLUTE_PATH = /\A (?:[A-Za-z]:|#{ SEPARATOR_PAT } )/
342346 else
343- ABSOLUTE_PATH = /\A #{ SEPARATOR_PAT } /o
347+ ABSOLUTE_PATH = /\A #{ SEPARATOR_PAT } /
344348 end
345349 private_constant :ABSOLUTE_PATH
346350
347351 # :startdoc:
348352
353+ # Creates a full path, including any intermediate directories that don't yet
354+ # exist.
355+ #
356+ # See FileUtils.mkpath and FileUtils.mkdir_p
357+ def mkpath ( mode : nil )
358+ path = @path == '/' ? @path : @path . chomp ( '/' )
359+
360+ stack = [ ]
361+ until File . directory? ( path ) || File . dirname ( path ) == path
362+ stack . push path
363+ path = File . dirname ( path )
364+ end
365+
366+ stack . reverse_each do |dir |
367+ dir = dir == '/' ? dir : dir . chomp ( '/' )
368+ if mode
369+ Dir . mkdir dir , mode
370+ File . chmod mode , dir
371+ else
372+ Dir . mkdir dir
373+ end
374+ rescue SystemCallError
375+ raise unless File . directory? ( dir )
376+ end
377+
378+ self
379+ end
380+
349381 # chop_basename(path) -> [pre-basename, basename] or nil
350382 def chop_basename ( path ) # :nodoc:
351383 base = File . basename ( path )
@@ -853,41 +885,39 @@ def relative_path_from(base_directory)
853885end
854886
855887class Pathname # * IO *
888+ # See <tt>IO.sysopen</tt>.
889+ def sysopen ( ...) IO . sysopen ( @path , ...) end
890+ end
891+
892+ class Pathname # * File *
856893 #
857894 # #each_line iterates over the line in the file. It yields a String object
858895 # for each line.
859896 #
860897 # This method has existed since 1.8.1.
861898 #
862899 def each_line ( ...) # :yield: line
863- IO . foreach ( @path , ...)
900+ File . foreach ( @path , ...)
864901 end
865902
866- # See <tt>IO .read</tt>. Returns all data from the file, or the first +N+ bytes
903+ # See <tt>File .read</tt>. Returns all data from the file, or the first +N+ bytes
867904 # if specified.
868- def read ( ...) IO . read ( @path , ...) end
905+ def read ( ...) File . read ( @path , ...) end
869906
870- # See <tt>IO .binread</tt>. Returns all the bytes from the file, or the first +N+
907+ # See <tt>File .binread</tt>. Returns all the bytes from the file, or the first +N+
871908 # if specified.
872- def binread ( ...) IO . binread ( @path , ...) end
909+ def binread ( ...) File . binread ( @path , ...) end
873910
874- # See <tt>IO.readlines</tt>. Returns all the lines from the file.
875- def readlines ( ...) IO . readlines ( @path , ...) end
876-
877- # See <tt>IO.sysopen</tt>.
878- def sysopen ( ...) IO . sysopen ( @path , ...) end
911+ # See <tt>File.readlines</tt>. Returns all the lines from the file.
912+ def readlines ( ...) File . readlines ( @path , ...) end
879913
880914 # Writes +contents+ to the file. See <tt>File.write</tt>.
881- def write ( ...) IO . write ( @path , ...) end
915+ def write ( ...) File . write ( @path , ...) end
882916
883917 # Writes +contents+ to the file, opening it in binary mode.
884918 #
885919 # See File.binwrite.
886- def binwrite ( ...) IO . binwrite ( @path , ...) end
887- end
888-
889-
890- class Pathname # * File *
920+ def binwrite ( ...) File . binwrite ( @path , ...) end
891921
892922 # See <tt>File.atime</tt>. Returns last access time.
893923 def atime ( ) File . atime ( @path ) end
@@ -1157,16 +1187,6 @@ def find(ignore_error: true) # :yield: pathname
11571187
11581188
11591189class Pathname # * FileUtils *
1160- # Creates a full path, including any intermediate directories that don't yet
1161- # exist.
1162- #
1163- # See FileUtils.mkpath and FileUtils.mkdir_p
1164- def mkpath ( mode : nil )
1165- require 'fileutils'
1166- FileUtils . mkpath ( @path , mode : mode )
1167- self
1168- end
1169-
11701190 # Recursively deletes a directory, including all directories beneath it.
11711191 #
11721192 # See FileUtils.rm_rf
@@ -1216,8 +1236,12 @@ module Kernel
12161236 #
12171237 # This method is available since 1.8.5.
12181238 def Pathname ( path ) # :doc:
1239+ Kernel . Pathname ( path )
1240+ end
1241+ private :Pathname
1242+
1243+ def self . Pathname ( path ) # Compatibility for C version
12191244 return path if Pathname === path
12201245 Pathname . new ( path )
12211246 end
1222- private :Pathname
12231247end
0 commit comments