Commit 51cf6b60 authored by Tomoyuki Sahara's avatar Tomoyuki Sahara Committed by GitHub

Merge pull request #65 from ksss/join

Support Array argument
parents d8189c2b ff845be7
...@@ -19,34 +19,47 @@ class File < IO ...@@ -19,34 +19,47 @@ class File < IO
end end
def self.join(*names) def self.join(*names)
if names.size == 0 return "" if names.empty?
""
elsif names.size == 1 names.map! do |name|
names[0] case name
else when String
if names[0][-1] == File::SEPARATOR name
s = names[0][0..-2] when Array
else if names == name
s = names[0].dup raise ArgumentError, "recursive array"
end
(1..names.size-2).each { |i|
t = names[i]
if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
t = t[1..-2]
elsif t[0] == File::SEPARATOR
t = t[1..-1]
elsif t[-1] == File::SEPARATOR
t = t[0..-2]
end end
s += File::SEPARATOR + t if t != "" join(*name)
}
if names[-1][0] == File::SEPARATOR
s += File::SEPARATOR + names[-1][1..-1]
else else
s += File::SEPARATOR + names[-1] raise TypeError, "no implicit conversion of #{name.class} into String"
end end
s
end end
return names[0] if names.size == 1
if names[0][-1] == File::SEPARATOR
s = names[0][0..-2]
else
s = names[0].dup
end
(1..names.size-2).each { |i|
t = names[i]
if t[0] == File::SEPARATOR and t[-1] == File::SEPARATOR
t = t[1..-2]
elsif t[0] == File::SEPARATOR
t = t[1..-1]
elsif t[-1] == File::SEPARATOR
t = t[0..-2]
end
s += File::SEPARATOR + t if t != ""
}
if names[-1][0] == File::SEPARATOR
s += File::SEPARATOR + names[-1][1..-1]
else
s += File::SEPARATOR + names[-1]
end
s
end end
def self.expand_path(path, default_dir = '.') def self.expand_path(path, default_dir = '.')
......
...@@ -69,14 +69,16 @@ assert('IO#flock') do ...@@ -69,14 +69,16 @@ assert('IO#flock') do
end end
assert('File.join') do assert('File.join') do
File.join() == "" and assert_equal "", File.join()
File.join("a") == "a" and assert_equal "a", File.join("a")
File.join("/a") == "/a" and assert_equal "/a", File.join("/a")
File.join("a/") == "a/" and assert_equal "a/", File.join("a/")
File.join("a", "b", "c") == "a/b/c" and assert_equal "a/b/c", File.join("a", "b", "c")
File.join("/a", "b", "c") == "/a/b/c" and assert_equal "/a/b/c", File.join("/a", "b", "c")
File.join("a", "b", "c/") == "a/b/c/" and assert_equal "a/b/c/", File.join("a", "b", "c/")
File.join("a/", "/b/", "/c") == "a/b/c" assert_equal "a/b/c", File.join("a/", "/b/", "/c")
assert_equal "a/b/c", File.join(["a", "b", "c"])
assert_equal "a/b/c", File.join("a", ["b", ["c"]])
end end
assert('File.realpath') do assert('File.realpath') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment