diff options
| author | Tomoyuki Sahara <[email protected]> | 2016-09-13 09:57:05 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2016-09-13 09:57:05 +0900 |
| commit | 51cf6b6048d7e4182d8bb8d58c7bcac07cb3064b (patch) | |
| tree | 4f34590b8e89469739b67a1b04687b377ba46ff2 | |
| parent | d8189c2b198b3441ac2d990b412b9c41447d75bc (diff) | |
| parent | ff845be72cc146dc8c16d408a5694e6587de5957 (diff) | |
| download | mruby-51cf6b6048d7e4182d8bb8d58c7bcac07cb3064b.tar.gz mruby-51cf6b6048d7e4182d8bb8d58c7bcac07cb3064b.zip | |
Merge pull request #65 from ksss/join
Support Array argument
| -rw-r--r-- | mrblib/file.rb | 61 | ||||
| -rw-r--r-- | test/file.rb | 18 |
2 files changed, 47 insertions, 32 deletions
diff --git a/mrblib/file.rb b/mrblib/file.rb index 86074c3e3..f84bd0ed3 100644 --- a/mrblib/file.rb +++ b/mrblib/file.rb @@ -19,34 +19,47 @@ class File < IO end def self.join(*names) - if names.size == 0 - "" - elsif names.size == 1 - names[0] - else - 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] + return "" if names.empty? + + names.map! do |name| + case name + when String + name + when Array + if names == name + raise ArgumentError, "recursive array" end - s += File::SEPARATOR + t if t != "" - } - if names[-1][0] == File::SEPARATOR - s += File::SEPARATOR + names[-1][1..-1] + join(*name) else - s += File::SEPARATOR + names[-1] + raise TypeError, "no implicit conversion of #{name.class} into String" end - s 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 def self.expand_path(path, default_dir = '.') diff --git a/test/file.rb b/test/file.rb index d62077f6e..48a6eea9d 100644 --- a/test/file.rb +++ b/test/file.rb @@ -69,14 +69,16 @@ assert('IO#flock') do end assert('File.join') do - File.join() == "" and - File.join("a") == "a" and - File.join("/a") == "/a" and - File.join("a/") == "a/" and - File.join("a", "b", "c") == "a/b/c" and - File.join("/a", "b", "c") == "/a/b/c" and - File.join("a", "b", "c/") == "a/b/c/" and - File.join("a/", "/b/", "/c") == "a/b/c" + assert_equal "", File.join() + assert_equal "a", File.join("a") + assert_equal "/a", File.join("/a") + assert_equal "a/", File.join("a/") + 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/") + 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 assert('File.realpath') do |
