Commit 44473fec authored by takahashim's avatar takahashim

add File.path

parent 828551f8
...@@ -170,4 +170,14 @@ class File < IO ...@@ -170,4 +170,14 @@ class File < IO
ext = fname.split('.').last ext = fname.split('.').last
ext.empty? ? '' : ".#{ext}" ext.empty? ? '' : ".#{ext}"
end end
def self.path(filename)
if filename.kind_of?(String)
filename
elsif filename.respond_to?(:to_path)
filename.to_path
else
raise TypeError, "no implicit conversion of #{filename.class} into String"
end
end
end end
...@@ -106,3 +106,12 @@ assert('File.expand_path (with ENV)') do ...@@ -106,3 +106,12 @@ assert('File.expand_path (with ENV)') do
assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir" assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
end end
assert('File.path') do
assert_equal "", File.path("")
assert_equal "a/b/c", File.path("a/b/c")
assert_equal "a/../b/./c", File.path("a/../b/./c")
assert_raise(TypeError) { File.path(nil) }
assert_raise(TypeError) { File.path(123) }
end
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