Method: IO.foreach
- Defined in:
- io.c
.foreach(name, sep = $/[, open_args]) {|line| ... } ⇒ nil .foreach(name, limit[, open_args]) {|line| ... } ⇒ nil .foreach(name, sep, limit[, open_args]) {|line| ... } ⇒ nil .foreach(...) ⇒ Object
Executes the block for every line in the named I/O port, where lines are separated by sep.
If no block is given, an enumerator is returned instead.
IO.foreach("testfile") {|x| print "GOT ", x }
produces:
GOT This is line one
GOT This is line two
GOT This is line three
GOT And so on...
If the last argument is a hash, it’s the keyword argument to open. See IO.read for detail.
9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 |
# File 'io.c', line 9746
static VALUE
rb_io_s_foreach(int argc, VALUE *argv, VALUE self)
{
VALUE opt;
int orig_argc = argc;
struct foreach_arg arg;
argc = rb_scan_args(argc, argv, "13:", NULL, NULL, NULL, NULL, &opt);
RETURN_ENUMERATOR(self, orig_argc, argv);
open_key_args(argc, argv, opt, &arg);
if (NIL_P(arg.io)) return Qnil;
return rb_ensure(io_s_foreach, (VALUE)&arg, rb_io_close, arg.io);
}
|