Merge pull request #6482 from dearblue/absolute_path

Add `File.absolute_path?` method
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-03-07 17:16:20 +09:00
committed by GitHub
2 changed files with 32 additions and 0 deletions
+9
View File
@@ -540,6 +540,14 @@ mrb_file_expand_path(mrb_state *mrb, mrb_value self)
return path_expand(mrb, path, default_dir);
}
static mrb_value
mrb_file_absolute_path_p(mrb_state *mrb, mrb_value klass)
{
mrb_value path = mrb_get_arg1(mrb);
mrb_ensure_string_type(mrb, path);
return mrb_bool_value(path_absolute_p(RSTRING_CSTR(mrb, path)));
}
#define TIME_OVERFLOW_P(a) (sizeof(time_t) >= sizeof(mrb_int) && ((a) > MRB_INT_MAX || (a) < MRB_INT_MIN))
#define TIME_T_UINT (~(time_t)0 > 0)
#if defined(MRB_USE_BITINT)
@@ -788,6 +796,7 @@ mrb_init_file(mrb_state *mrb)
mrb_define_class_method_id(mrb, file, MRB_SYM(dirname), mrb_file_dirname, MRB_ARGS_REQ(1));
mrb_define_class_method_id(mrb, file, MRB_SYM(basename), mrb_file_basename, MRB_ARGS_REQ(1));
mrb_define_class_method_id(mrb, file, MRB_SYM(realpath), mrb_file_realpath, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
mrb_define_class_method_id(mrb, file, MRB_SYM_Q(absolute_path), mrb_file_absolute_path_p, MRB_ARGS_REQ(1));
mrb_define_class_method_id(mrb, file, MRB_SYM(expand_path), mrb_file_expand_path, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1));
mrb_define_method_id(mrb, file, MRB_SYM(flock), mrb_file_flock, MRB_ARGS_REQ(1));
+23
View File
@@ -303,6 +303,29 @@ assert('File.expand_path (with getenv(3))') do
assert_equal "#{MRubyIOTestUtil::ENV_HOME}/user", File.expand_path("user", MRubyIOTestUtil::ENV_HOME), "relative with base_dir"
end
if MRubyIOTestUtil.win?
assert('File.absolute_path? (for windows)') do
assert_true File.absolute_path?("c:/")
assert_true File.absolute_path?("c:/a")
assert_true File.absolute_path?("//")
assert_true File.absolute_path?("//?")
assert_true File.absolute_path?("//?/")
assert_false File.absolute_path?("")
assert_false File.absolute_path?("/")
assert_false File.absolute_path?("c:")
end
else
assert('File.absolute_path?') do
assert_true File.absolute_path?("/")
assert_true File.absolute_path?("/a")
assert_true File.absolute_path?("/a/b/")
assert_false File.absolute_path?("")
assert_false File.absolute_path?("a")
assert_false File.absolute_path?("a/b/")
assert_false File.absolute_path?("c:/")
end
end
assert('File.path') do
assert_equal "", File.path("")
assert_equal "a/b/c", File.path("a/b/c")