22
33static VALUE rb_cPathname ;
44static ID id_at_path ;
5+ static ID id_sub ;
56
67static VALUE
78get_strpath (VALUE obj )
@@ -61,6 +62,27 @@ path_cmp(VALUE self, VALUE other)
6162 return INT2FIX (0 );
6263}
6364
65+ /*
66+ * Return a pathname which is substituted by String#sub.
67+ *
68+ * path1 = Pathname.new('/usr/bin/perl')
69+ * path1.sub('perl', 'ruby')
70+ * #=> #<Pathname:/usr/bin/ruby>
71+ */
72+ static VALUE
73+ path_sub (int argc , VALUE * argv , VALUE self )
74+ {
75+ VALUE str = get_strpath (self );
76+
77+ if (rb_block_given_p ()) {
78+ str = rb_block_call (str , id_sub , argc , argv , 0 , 0 );
79+ }
80+ else {
81+ str = rb_funcallv (str , id_sub , argc , argv );
82+ }
83+ return rb_class_new_instance (1 , & str , rb_obj_class (self ));
84+ }
85+
6486static void init_ids (void );
6587
6688void
@@ -79,11 +101,13 @@ InitVM_pathname(void)
79101{
80102 rb_cPathname = rb_define_class ("Pathname" , rb_cObject );
81103 rb_define_method (rb_cPathname , "<=>" , path_cmp , 1 );
104+ rb_define_method (rb_cPathname , "sub" , path_sub , -1 );
82105}
83106
84107void
85108init_ids (void )
86109{
87110#undef rb_intern
88111 id_at_path = rb_intern ("@path" );
112+ id_sub = rb_intern ("sub" );
89113}
0 commit comments