From 44473fecdd454574b8cab67da13c2d5bb809bee2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 21 Nov 2015 22:03:40 +0900 Subject: add File.path --- mrblib/file.rb | 10 ++++++++++ test/file.rb | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/mrblib/file.rb b/mrblib/file.rb index aa0981930..f644ff691 100644 --- a/mrblib/file.rb +++ b/mrblib/file.rb @@ -170,4 +170,14 @@ class File < IO ext = fname.split('.').last ext.empty? ? '' : ".#{ext}" 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 diff --git a/test/file.rb b/test/file.rb index d6f39ceb1..487b54f38 100644 --- a/test/file.rb +++ b/test/file.rb @@ -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" 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 -- cgit v1.2.3