1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/pathname.rb: use a subclass for instantiation except

methods take pathname argument.  suggested by Evan Phoenix.
  [ruby-core:7618]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10076 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2006-04-04 15:29:59 +00:00
parent ec8e6b07ba
commit 62123eff4f
2 changed files with 29 additions and 21 deletions

View file

@ -1,3 +1,9 @@
Wed Apr 5 00:22:54 2006 Tanaka Akira <akr@m17n.org>
* lib/pathname.rb: use a subclass for instantiation except
methods take pathname argument. suggested by Evan Phoenix.
[ruby-core:7618]
Sat Apr 1 15:11:27 2006 Masaki Suketa <masaki.suketa@nifty.ne.jp> Sat Apr 1 15:11:27 2006 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c :add WIN32OLE_TYPE#inspect, * ext/win32ole/win32ole.c :add WIN32OLE_TYPE#inspect,

View file

@ -237,7 +237,7 @@ class Pathname
# Return a pathname which is substituted by String#sub. # Return a pathname which is substituted by String#sub.
def sub(pattern, *rest, &block) def sub(pattern, *rest, &block)
Pathname.new(@path.sub(pattern, *rest, &block)) self.class.new(@path.sub(pattern, *rest, &block))
end end
if File::ALT_SEPARATOR if File::ALT_SEPARATOR
@ -322,7 +322,7 @@ class Pathname
if /#{SEPARATOR_PAT}/o =~ File.basename(pre) if /#{SEPARATOR_PAT}/o =~ File.basename(pre)
names.shift while names[0] == '..' names.shift while names[0] == '..'
end end
Pathname.new(prepend_prefix(pre, File.join(*names))) self.class.new(prepend_prefix(pre, File.join(*names)))
end end
private :cleanpath_aggressive private :cleanpath_aggressive
@ -371,16 +371,16 @@ class Pathname
names.shift while names[0] == '..' names.shift while names[0] == '..'
end end
if names.empty? if names.empty?
File.dirname(pre) self.class.new(File.dirname(pre))
else else
if names.last != '..' && File.basename(path) == '.' if names.last != '..' && File.basename(path) == '.'
names << '.' names << '.'
end end
result = prepend_prefix(pre, File.join(*names)) result = prepend_prefix(pre, File.join(*names))
if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path) if /\A(?:\.|\.\.)\z/ !~ names.last && has_trailing_separator?(path)
Pathname.new(add_trailing_separator(result)) self.class.new(add_trailing_separator(result))
else else
Pathname.new(result) self.class.new(result)
end end
end end
end end
@ -437,7 +437,7 @@ class Pathname
names = names2 + names names = names2 + names
end end
prefix, *names = realpath_rec(prefix, names, {}) prefix, *names = realpath_rec(prefix, names, {})
Pathname.new(prepend_prefix(prefix, File.join(*names))) self.class.new(prepend_prefix(prefix, File.join(*names)))
end end
# #parent returns the parent directory. # #parent returns the parent directory.
@ -518,6 +518,7 @@ class Pathname
vs = [] vs = []
ascend {|v| vs << v } ascend {|v| vs << v }
vs.reverse_each {|v| yield v } vs.reverse_each {|v| yield v }
nil
end end
# Iterates over and yields a new Pathname object # Iterates over and yields a new Pathname object
@ -544,7 +545,7 @@ class Pathname
while r = chop_basename(path) while r = chop_basename(path)
path, name = r path, name = r
break if path.empty? break if path.empty?
yield Pathname.new(del_trailing_separator(path)) yield self.class.new(del_trailing_separator(path))
end end
end end
@ -603,6 +604,7 @@ class Pathname
r1 ? prefix1 : File.dirname(prefix1) r1 ? prefix1 : File.dirname(prefix1)
end end
end end
private :plus
# #
# Pathname#join joins pathnames. # Pathname#join joins pathnames.
@ -650,9 +652,9 @@ class Pathname
Dir.foreach(@path) {|e| Dir.foreach(@path) {|e|
next if e == '.' || e == '..' next if e == '.' || e == '..'
if with_directory if with_directory
result << Pathname.new(File.join(@path, e)) result << self.class.new(File.join(@path, e))
else else
result << Pathname.new(e) result << self.class.new(e)
end end
} }
result result
@ -778,7 +780,7 @@ class Pathname # * File *
end end
# See <tt>File.readlink</tt>. Read symbolic link. # See <tt>File.readlink</tt>. Read symbolic link.
def readlink() Pathname.new(File.readlink(@path)) end def readlink() self.class.new(File.readlink(@path)) end
# See <tt>File.rename</tt>. Rename the file. # See <tt>File.rename</tt>. Rename the file.
def rename(to) File.rename(@path, to) end def rename(to) File.rename(@path, to) end
@ -799,20 +801,20 @@ class Pathname # * File *
def utime(atime, mtime) File.utime(atime, mtime, @path) end def utime(atime, mtime) File.utime(atime, mtime, @path) end
# See <tt>File.basename</tt>. Returns the last component of the path. # See <tt>File.basename</tt>. Returns the last component of the path.
def basename(*args) Pathname.new(File.basename(@path, *args)) end def basename(*args) self.class.new(File.basename(@path, *args)) end
# See <tt>File.dirname</tt>. Returns all but the last component of the path. # See <tt>File.dirname</tt>. Returns all but the last component of the path.
def dirname() Pathname.new(File.dirname(@path)) end def dirname() self.class.new(File.dirname(@path)) end
# See <tt>File.extname</tt>. Returns the file's extension. # See <tt>File.extname</tt>. Returns the file's extension.
def extname() File.extname(@path) end def extname() File.extname(@path) end
# See <tt>File.expand_path</tt>. # See <tt>File.expand_path</tt>.
def expand_path(*args) Pathname.new(File.expand_path(@path, *args)) end def expand_path(*args) self.class.new(File.expand_path(@path, *args)) end
# See <tt>File.split</tt>. Returns the #dirname and the #basename in an # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
# Array. # Array.
def split() File.split(@path).map {|f| Pathname.new(f) } end def split() File.split(@path).map {|f| self.class.new(f) } end
# Pathname#link is confusing and *obsoleted* because the receiver/argument # Pathname#link is confusing and *obsoleted* because the receiver/argument
# order is inverted to corresponding system call. # order is inverted to corresponding system call.
@ -910,14 +912,14 @@ class Pathname # * Dir *
# See <tt>Dir.glob</tt>. Returns or yields Pathname objects. # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
def Pathname.glob(*args) # :yield: p def Pathname.glob(*args) # :yield: p
if block_given? if block_given?
Dir.glob(*args) {|f| yield Pathname.new(f) } Dir.glob(*args) {|f| yield self.new(f) }
else else
Dir.glob(*args).map {|f| Pathname.new(f) } Dir.glob(*args).map {|f| self.new(f) }
end end
end end
# See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname. # See <tt>Dir.getwd</tt>. Returns the current working directory as a Pathname.
def Pathname.getwd() Pathname.new(Dir.getwd) end def Pathname.getwd() self.new(Dir.getwd) end
class << self; alias pwd getwd end class << self; alias pwd getwd end
# Pathname#chdir is *obsoleted* at 1.8.1. # Pathname#chdir is *obsoleted* at 1.8.1.
@ -934,14 +936,14 @@ class Pathname # * Dir *
# Return the entries (files and subdirectories) in the directory, each as a # Return the entries (files and subdirectories) in the directory, each as a
# Pathname object. # Pathname object.
def entries() Dir.entries(@path).map {|f| Pathname.new(f) } end def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
# Iterates over the entries (files and subdirectories) in the directory. It # Iterates over the entries (files and subdirectories) in the directory. It
# yields a Pathname object for each entry. # yields a Pathname object for each entry.
# #
# This method has existed since 1.8.1. # This method has existed since 1.8.1.
def each_entry(&block) # :yield: p def each_entry(&block) # :yield: p
Dir.foreach(@path) {|f| yield Pathname.new(f) } Dir.foreach(@path) {|f| yield self.class.new(f) }
end end
# Pathname#dir_foreach is *obsoleted* at 1.8.1. # Pathname#dir_foreach is *obsoleted* at 1.8.1.
@ -977,9 +979,9 @@ class Pathname # * Find *
def find(&block) # :yield: p def find(&block) # :yield: p
require 'find' require 'find'
if @path == '.' if @path == '.'
Find.find(@path) {|f| yield Pathname.new(f.sub(%r{\A\./}, '')) } Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
else else
Find.find(@path) {|f| yield Pathname.new(f) } Find.find(@path) {|f| yield self.class.new(f) }
end end
end end
end end