4 def initialize(fd_or_path, mode = "r", perm = 0666)
5 if fd_or_path.kind_of? Fixnum
6 super(fd_or_path, mode)
9 fd = IO.sysopen(@path, mode, perm)
15 return "" if names.empty?
23 raise ArgumentError, "recursive array"
27 raise TypeError, "no implicit conversion of #{name.class} into String"
31 return names[0] if names.size == 1
33 if names[0][-1] == File::SEPARATOR
39 (1..names.size-2).each { |i|
41 if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
43 elsif t[0] == File::SEPARATOR
45 elsif t[-1] == File::SEPARATOR
48 s += File::SEPARATOR + t if t != ""
50 if names[-1][0] == File::SEPARATOR
51 s += File::SEPARATOR + names[-1][1..-1]
53 s += File::SEPARATOR + names[-1]
58 def self._concat_path(path, base_path)
59 if path[0] == "/" || path[1] == ':' # Windows root!
62 if (path[1] == "/" || path[1] == nil)
63 dir = path[1, path.size]
67 raise ArgumentError, "couldn't find HOME environment -- expanding '~'"
70 expanded_path = home_dir
71 expanded_path += dir if dir
74 splitted_path = path.split("/")
75 user = splitted_path[0][1, splitted_path[0].size]
76 dir = "/" + splitted_path[1, splitted_path.size].join("/")
78 home_dir = _gethome(user)
81 raise ArgumentError, "user #{user} doesn't exist"
84 expanded_path = home_dir
85 expanded_path += dir if dir
89 expanded_path = _concat_path(base_path, _getwd)
90 expanded_path += "/" + path
96 def self.expand_path(path, default_dir = '.')
97 expanded_path = _concat_path(path, default_dir)
99 if File::ALT_SEPARATOR && expanded_path.size > 2 &&
100 ("A".."Z").include?(expanded_path[0].upcase) && expanded_path[1] == ":"
101 drive_prefix = expanded_path[0, 2]
102 expanded_path = expanded_path[2, expanded_path.size]
104 expand_path_array = []
105 if File::ALT_SEPARATOR && expanded_path.include?(File::ALT_SEPARATOR)
106 expanded_path.gsub!(File::ALT_SEPARATOR, '/')
108 while expanded_path.include?('//')
109 expanded_path = expanded_path.gsub('//', '/')
112 if expanded_path != "/"
113 expanded_path.split('/').each do |path_token|
114 if path_token == '..'
115 if expand_path_array.size > 1
116 expand_path_array.pop
118 elsif path_token == '.'
121 expand_path_array << path_token
125 expanded_path = expand_path_array.join("/")
126 if expanded_path.empty?
130 if drive_prefix.empty?
133 drive_prefix + expanded_path.gsub("/", File::ALT_SEPARATOR)
137 def self.foreach(file)
139 self.open(file) do |f|
143 return self.new(file)
147 def self.directory?(file)
148 FileTest.directory?(file)
151 def self.exist?(file)
152 FileTest.exist?(file)
155 def self.exists?(file)
156 FileTest.exists?(file)
175 def self.socket?(file)
176 FileTest.socket?(file)
179 def self.symlink?(file)
180 FileTest.symlink?(file)
187 def self.extname(filename)
188 fname = self.basename(filename)
189 return '' if fname[0] == '.' || fname.index('.').nil?
190 ext = fname.split('.').last
191 ext.empty? ? '' : ".#{ext}"
194 def self.path(filename)
195 if filename.kind_of?(String)
197 elsif filename.respond_to?(:to_path)
200 raise TypeError, "no implicit conversion of #{filename.class} into String"