summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog7
-rw-r--r--io.c33
-rw-r--r--string.c8
3 files changed, 44 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index eb1aa26054..0103156e0e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Tue Oct 17 08:04:31 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * string.c (rb_str_lines): now takes optional argument for the
+ line separator.
+
+ * io.c (rb_io_lines, rb_io_bytes): new methods.
+
Mon Oct 16 23:33:18 2006 Yukihiro Matsumoto <matz@ruby-lang.org>
* array.c (rb_ary_unshift_m): a bug in lfree shift length
diff --git a/io.c b/io.c
index 342fb5697a..a78259ba51 100644
--- a/io.c
+++ b/io.c
@@ -1961,6 +1961,37 @@ rb_io_each_byte(VALUE io)
return io;
}
+/*
+ * call-seq:
+ * str.lines(separator=$/) => anEnumerator
+ *
+ * Returns an enumerator that gives each line in the string.
+ *
+ * "foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"]
+ * "foo\nb ar".lines.sort #=> ["b ar", "foo\n"]
+ */
+
+static VALUE
+rb_io_lines(int argc, VALUE *argv, VALUE str)
+{
+ return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), argc, argv);
+}
+
+/*
+ * call-seq:
+ * str.bytes => anEnumerator
+ *
+ * Returns an enumerator that gives each byte in the string.
+ *
+ * "hello".bytes.to_a #=> [104, 101, 108, 108, 111]
+ */
+
+static VALUE
+rb_io_bytes(VALUE str)
+{
+ return rb_enumeratorize(str, ID2SYM(rb_intern("each_byte")), 0, 0);
+}
+
VALUE
rb_io_getc(VALUE io)
{
@@ -5639,6 +5670,8 @@ Init_IO(void)
rb_define_method(rb_cIO, "each", rb_io_each_line, -1);
rb_define_method(rb_cIO, "each_line", rb_io_each_line, -1);
rb_define_method(rb_cIO, "each_byte", rb_io_each_byte, 0);
+ rb_define_method(rb_cIO, "lines", rb_io_lines, -1);
+ rb_define_method(rb_cIO, "bytes", rb_io_bytes, 0);
rb_define_method(rb_cIO, "syswrite", rb_io_syswrite, 1);
rb_define_method(rb_cIO, "sysread", rb_io_sysread, -1);
diff --git a/string.c b/string.c
index b7466f9177..13c051f9f2 100644
--- a/string.c
+++ b/string.c
@@ -3550,7 +3550,7 @@ rb_str_split(VALUE str, const char *sep0)
/*
* call-seq:
- * str.lines => anEnumerator
+ * str.lines(separator=$/) => anEnumerator
*
* Returns an enumerator that gives each line in the string.
*
@@ -3559,9 +3559,9 @@ rb_str_split(VALUE str, const char *sep0)
*/
static VALUE
-rb_str_lines(VALUE str)
+rb_str_lines(int argc, VALUE *argv, VALUE str)
{
- return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), 0, 0);
+ return rb_enumeratorize(str, ID2SYM(rb_intern("each_line")), argc, argv);
}
/*
@@ -4881,7 +4881,7 @@ Init_String(void)
rb_define_method(rb_cString, "hex", rb_str_hex, 0);
rb_define_method(rb_cString, "oct", rb_str_oct, 0);
rb_define_method(rb_cString, "split", rb_str_split_m, -1);
- rb_define_method(rb_cString, "lines", rb_str_lines, 0);
+ rb_define_method(rb_cString, "lines", rb_str_lines, -1);
rb_define_method(rb_cString, "bytes", rb_str_bytes, 0);
rb_define_method(rb_cString, "reverse", rb_str_reverse, 0);
rb_define_method(rb_cString, "reverse!", rb_str_reverse_bang, 0);