[前][次][番号順一覧][スレッド一覧]

ruby-changes:17079

From: akr <ko1@a...>
Date: Mon, 23 Aug 2010 23:18:58 +0900 (JST)
Subject: [ruby-changes:17079] Ruby:r29077 (trunk): * ext/pathname/pathname.c (path_each_line): Pathname#each_line

akr	2010-08-23 23:16:06 +0900 (Mon, 23 Aug 2010)

  New Revision: 29077

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29077

  Log:
    * ext/pathname/pathname.c (path_each_line): Pathname#each_line
      translated from pathname.rb.

  Modified files:
    trunk/ChangeLog
    trunk/ext/pathname/lib/pathname.rb
    trunk/ext/pathname/pathname.c
    trunk/test/pathname/test_pathname.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 29076)
+++ ChangeLog	(revision 29077)
@@ -1,3 +1,8 @@
+Mon Aug 23 23:14:21 2010  Tanaka Akira  <akr@f...>
+
+	* ext/pathname/pathname.c (path_each_line): Pathname#each_line
+	  translated from pathname.rb.
+
 Mon Aug 23 22:30:58 2010  Nobuyoshi Nakada  <nobu@r...>
 
 	* common.mk (version.o): depends on both of version.h and
Index: ext/pathname/lib/pathname.rb
===================================================================
--- ext/pathname/lib/pathname.rb	(revision 29076)
+++ ext/pathname/lib/pathname.rb	(revision 29077)
@@ -484,16 +484,6 @@
 end
 
 class Pathname    # * IO *
-  #
-  # #each_line iterates over the line in the file.  It yields a String object
-  # for each line.
-  #
-  # This method has existed since 1.8.1.
-  #
-  def each_line(*args, &block) # :yield: line
-    IO.foreach(@path, *args, &block)
-  end
-
   # See <tt>IO.read</tt>.  Returns all data from the file, or the first +N+ bytes
   # if specified.
   def read(*args) IO.read(@path, *args) end
Index: ext/pathname/pathname.c
===================================================================
--- ext/pathname/pathname.c	(revision 29076)
+++ ext/pathname/pathname.c	(revision 29077)
@@ -233,6 +233,35 @@
 }
 
 /*
+ * call-seq:
+ *   pathname.each_line {|line| ... }
+ *   pathname.each_line(sep=$/ [, open_args]) {|line| block }     -> nil
+ *   pathname.each_line(limit [, open_args]) {|line| block }      -> nil
+ *   pathname.each_line(sep, limit [, open_args]) {|line| block } -> nil
+ *   pathname.each_line(...)                                      -> an_enumerator
+ *        
+ * #each_line iterates over the line in the file.  It yields a String object
+ * for each line.
+ * 
+ * This method is availabel since 1.8.1.
+ */
+static VALUE
+path_each_line(int argc, VALUE *argv, VALUE self)
+{
+    VALUE args[4];
+    int n;
+
+    args[0] = get_strpath(self);
+    n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
+    if (rb_block_given_p()) {
+        return rb_block_call(rb_cIO, rb_intern("foreach"), 1+n, args, 0, 0);
+    }
+    else {
+        return rb_funcall2(rb_cIO, rb_intern("foreach"), 1+n, args);
+    }
+}
+
+/*
  * See <tt>File.atime</tt>.  Returns last access time.
  */
 static VALUE
@@ -691,6 +720,7 @@
     rb_define_method(rb_cPathname, "sub_ext", path_sub_ext, 1);
     rb_define_method(rb_cPathname, "realpath", path_realpath, -1);
     rb_define_method(rb_cPathname, "realdirpath", path_realdirpath, -1);
+    rb_define_method(rb_cPathname, "each_line", path_each_line, -1);
     rb_define_method(rb_cPathname, "atime", path_atime, 0);
     rb_define_method(rb_cPathname, "ctime", path_ctime, 0);
     rb_define_method(rb_cPathname, "mtime", path_mtime, 0);
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 29076)
+++ test/pathname/test_pathname.rb	(revision 29077)
@@ -670,6 +670,23 @@
       a = []
       Pathname("a").each_line {|line| a << line }
       assert_equal(["1\n", "2\n"], a)
+
+      a = []
+      Pathname("a").each_line("2") {|line| a << line }
+      assert_equal(["1\n2", "\n"], a)
+
+      a = []
+      Pathname("a").each_line(1) {|line| a << line }
+      assert_equal(["1", "\n", "2", "\n"], a)
+
+      a = []
+      Pathname("a").each_line("2", 1) {|line| a << line }
+      assert_equal(["1", "\n", "2", "\n"], a)
+
+      a = []
+      enum = Pathname("a").each_line
+      enum.each {|line| a << line }
+      assert_equal(["1\n", "2\n"], a)
     }
   end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]