From 1d3757333b877a1951940a45ba9890af2671cee1 Mon Sep 17 00:00:00 2001 From: "Alexander E. Fischer" Date: Thu, 17 Nov 2011 11:14:18 +0100 Subject: [PATCH 1/3] Implemented a way to glob based on an existing pathname object --- ext/pathname/lib/pathname.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ext/pathname/lib/pathname.rb b/ext/pathname/lib/pathname.rb index 90485a27838f29..bce1714949cf83 100644 --- a/ext/pathname/lib/pathname.rb +++ b/ext/pathname/lib/pathname.rb @@ -74,6 +74,11 @@ def prepend_prefix(prefix, relpath) end private :prepend_prefix + # See Dir.glob. Returns or yields Pathname objects. + def glob(*args) + self.class.glob(self + args.shift, *args) + end + # Returns clean pathname of +self+ with consecutive slashes and useless dots # removed. The filesystem is not accessed. # From 7d455e69e9521416e6b3714eca9816967775041a Mon Sep 17 00:00:00 2001 From: "Alexander E. Fischer" Date: Thu, 17 Nov 2011 11:16:54 +0100 Subject: [PATCH 2/3] Made it possible to use Pathname.[] as an alternative to Pathname.new. Deprecated the global Pathname() method, because I consider it bad style to poison the global namespace with things like this. --- ext/pathname/pathname.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index e1632c524b545c..d6379c96664d80 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -857,6 +857,15 @@ path_s_getwd(VALUE klass) return rb_class_new_instance(1, &str, klass); } +/* + * Returns a new instance of Pathname initialized with string + */ +static VALUE +path_s_square_brackets(VALUE self, VALUE string) +{ + return rb_class_new_instance(1, &string, rb_cPathname); +} + /* * Return the entries (files and subdirectories) in the directory, each as a * Pathname object. @@ -969,6 +978,7 @@ path_unlink(VALUE self) static VALUE path_f_pathname(VALUE self, VALUE str) { + rb_warn("Kernel#Pathname is deprecated; use Pathname.[]"); return rb_class_new_instance(1, &str, rb_cPathname); } @@ -1225,6 +1235,7 @@ Init_pathname() rb_define_method(rb_cPathname, "world_writable?", path_world_writable_p, 0); rb_define_method(rb_cPathname, "writable_real?", path_writable_real_p, 0); rb_define_method(rb_cPathname, "zero?", path_zero_p, 0); + rb_define_singleton_method(rb_cPathname, "[]", path_s_square_brackets, 1); rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1); rb_define_singleton_method(rb_cPathname, "getwd", path_s_getwd, 0); rb_define_singleton_method(rb_cPathname, "pwd", path_s_getwd, 0); From 2a532f4e13fa60c64d2e386db7c714d8db62d0c3 Mon Sep 17 00:00:00 2001 From: "Alexander E. Fischer" Date: Thu, 17 Nov 2011 21:09:10 +0100 Subject: [PATCH 3/3] Slightly changed deprecation warning to match the style of others --- ext/pathname/pathname.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/pathname/pathname.c b/ext/pathname/pathname.c index d6379c96664d80..3d1be2000dc192 100644 --- a/ext/pathname/pathname.c +++ b/ext/pathname/pathname.c @@ -978,7 +978,7 @@ path_unlink(VALUE self) static VALUE path_f_pathname(VALUE self, VALUE str) { - rb_warn("Kernel#Pathname is deprecated; use Pathname.[]"); + rb_warn("Kernel#Pathname is deprecated; use Pathname.[] instead"); return rb_class_new_instance(1, &str, rb_cPathname); }