summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-16 23:07:07 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-10-16 23:07:07 +0000
commit24691fa633a71d5ea831e23b83886a1ce6ef0117 (patch)
treeea46012711fae60c2fc767591797305ec842b036 /io.c
parent58c0100a779a354cd53a53cf1c2fbc6cfc88240f (diff)
* string.c (rb_str_lines): now takes optional argument for the
line separator. * io.c (rb_io_lines, rb_io_bytes): new methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11180 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c33
1 files changed, 33 insertions, 0 deletions
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);