summaryrefslogtreecommitdiff
path: root/file.c
diff options
context:
space:
mode:
authorDavid Rodríguez <deivid.rodriguez@riseup.net>2019-09-05 13:00:50 +0200
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-09-05 20:00:50 +0900
commit2a166cfea22b90e39e3fe9bafab6b806ed4813f6 (patch)
treed08e04f9c8e56e70a959733f50c8ec4903336f4a /file.c
parentd9e6315177be2a1264213a1e7cb215312a23384a (diff)
Add `File.absolute_path?` (#2198)
In order to check whether a path is absolute or not in a portable way. [Feature #15868]
Diffstat (limited to 'file.c')
-rw-r--r--file.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/file.c b/file.c
index b1ba4c3cfc2..1c14c7e7025 100644
--- a/file.c
+++ b/file.c
@@ -4106,6 +4106,25 @@ s_absolute_path(int c, const VALUE * v, VALUE _)
return rb_file_s_absolute_path(c, v);
}
+/*
+ * call-seq:
+ * File.absolute_path?(file_name) -> true or false
+ *
+ * Returns <code>true</code> if +file_name+ is an absolute path, and
+ * <code>false</code> otherwise.
+ *
+ * File.absolute_path?("c:/foo") #=> false (on Linux), true (on Windows)
+ */
+
+static VALUE
+s_absolute_path_p(VALUE klass, VALUE fname)
+{
+ VALUE path = rb_get_path(fname);
+
+ if (!rb_is_absolute_path(RSTRING_PTR(path))) return Qfalse;
+ return Qtrue;
+}
+
enum rb_realpath_mode {
RB_REALPATH_CHECK,
RB_REALPATH_DIR,
@@ -6504,6 +6523,7 @@ Init_File(void)
rb_define_singleton_method(rb_cFile, "mkfifo", rb_file_s_mkfifo, -1);
rb_define_singleton_method(rb_cFile, "expand_path", s_expand_path, -1);
rb_define_singleton_method(rb_cFile, "absolute_path", s_absolute_path, -1);
+ rb_define_singleton_method(rb_cFile, "absolute_path?", s_absolute_path_p, 1);
rb_define_singleton_method(rb_cFile, "realpath", rb_file_s_realpath, -1);
rb_define_singleton_method(rb_cFile, "realdirpath", rb_file_s_realdirpath, -1);
rb_define_singleton_method(rb_cFile, "basename", rb_file_s_basename, -1);