@@ -12,6 +12,7 @@ static ID id_binwrite;
1212static ID id_birthtime ;
1313static ID id_blockdev_p ;
1414static ID id_chardev_p ;
15+ static ID id_chdir ;
1516static ID id_chmod ;
1617static ID id_chown ;
1718static ID id_ctime ;
@@ -1248,6 +1249,24 @@ path_opendir(VALUE self)
12481249 return rb_block_call (rb_cDir , id_open , 1 , args , 0 , 0 );
12491250}
12501251
1252+ /*
1253+ * Changes the current path to the referenced directory.
1254+ *
1255+ * See Dir.chdir.
1256+ */
1257+ static VALUE
1258+ path_chdir (VALUE self )
1259+ {
1260+ VALUE args [1 ];
1261+
1262+ if (rb_block_given_p ()) {
1263+ args [0 ] = get_strpath (self );
1264+ return rb_block_call (rb_cDir , id_chdir , 1 , args , 0 , 0 );
1265+ } else {
1266+ return rb_funcall (rb_cDir , id_chdir , 1 , get_strpath (self ));
1267+ }
1268+ }
1269+
12511270static VALUE
12521271each_entry_i (RB_BLOCK_CALL_FUNC_ARGLIST (elt , klass ))
12531272{
@@ -1470,6 +1489,7 @@ path_f_pathname(VALUE self, VALUE str)
14701489 * - #each_entry(&block)
14711490 * - #mkdir(*args)
14721491 * - #opendir(*args)
1492+ * - #chdir(*args)
14731493 *
14741494 * === IO
14751495 *
@@ -1589,6 +1609,7 @@ Init_pathname(void)
15891609 rb_define_method (rb_cPathname , "mkdir" , path_mkdir , -1 );
15901610 rb_define_method (rb_cPathname , "rmdir" , path_rmdir , 0 );
15911611 rb_define_method (rb_cPathname , "opendir" , path_opendir , 0 );
1612+ rb_define_method (rb_cPathname , "chdir" , path_chdir , 0 );
15921613 rb_define_method (rb_cPathname , "each_entry" , path_each_entry , 0 );
15931614 rb_define_method (rb_cPathname , "unlink" , path_unlink , 0 );
15941615 rb_define_method (rb_cPathname , "delete" , path_unlink , 0 );
@@ -1611,6 +1632,7 @@ InitVM_pathname(void)
16111632 id_birthtime = rb_intern ("birthtime" );
16121633 id_blockdev_p = rb_intern ("blockdev?" );
16131634 id_chardev_p = rb_intern ("chardev?" );
1635+ id_chdir = rb_intern ("chdir" );
16141636 id_chmod = rb_intern ("chmod" );
16151637 id_chown = rb_intern ("chown" );
16161638 id_ctime = rb_intern ("ctime" );
0 commit comments