Method: Dir.foreach

Defined in:
dir.c

.foreach(dirname) {|filename| ... } ⇒ nil .foreach(dirname, encoding: enc) {|filename| ... } ⇒ nil .foreach(dirname) ⇒ Object .foreach(dirname, encoding: enc) ⇒ Object

Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.

If no block is given, an enumerator is returned instead.

Dir.foreach("testdir") {|x| puts "Got #{x}" }

produces:

Got .
Got ..
Got config.h
Got main.rb

Overloads:

  • .foreach(dirname) {|filename| ... } ⇒ nil

    Yields:

    • (filename)

    Returns:

    • (nil)
  • .foreach(dirname, encoding: enc) {|filename| ... } ⇒ nil

    Yields:

    • (filename)

    Returns:

    • (nil)


2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
# File 'dir.c', line 2943

static VALUE
dir_foreach(int argc, VALUE *argv, VALUE io)
{
    VALUE dir;

    RETURN_ENUMERATOR(io, argc, argv);
    dir = dir_open_dir(argc, argv);
    rb_ensure(dir_each, dir, dir_close, dir);
    return Qnil;
}