foreach

IO.foreach(name, sep=$/ [, open_args]) {|line| block } â nil
IO.foreach(name, limit [, open_args]) {|line| block } â nil
IO.foreach(name, sep, limit [, open_args]) {|line| block } â nil
IO.foreach(...) â an_enumerator
Class Public methods

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.

doc_ruby_on_rails
2015-04-13 09:01:10
Comments
Leave a Comment

Please login to continue.