Loop through files in a directory

Discussion in 'Ruby' started by Ben Johnson, May 4, 2007.

  1. Ben Johnson

    Ben Johnson Guest

    I know this is a nub question, but the documentation on ruby is
    terrible.

    How do I loop through all of the files in a directory?

    Thanks for your help.

    --
    Posted via http://www.ruby-forum.com/.
     
    Ben Johnson, May 4, 2007
    #1
    1. Advertising

  2. Ben Johnson

    Jano Svitok Guest

    On 5/4/07, Ben Johnson <> wrote:
    > I know this is a nub question, but the documentation on ruby is
    > terrible.
    >
    > How do I loop through all of the files in a directory?
    >
    > Thanks for your help.
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    Have a look at Find.find and Dir.each.
     
    Jano Svitok, May 4, 2007
    #2
    1. Advertising

  3. Ben Johnson

    Guest

    On Sat, 5 May 2007, Ben Johnson wrote:

    > I know this is a nub question, but the documentation on ruby is
    > terrible.
    >
    > How do I loop through all of the files in a directory?
    >
    > Thanks for your help.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >



    harp:~ > ls dir
    a b c


    harp:~ > cat a.rb
    class Dir
    def self.ls dir, glob = File.join('**', '**'), &block
    ret = [] unless block
    Dir.glob(File.join(dir, glob)) do |entry|
    block ? block.call(entry) : ret.push(entry)
    end
    ret
    end
    end

    Dir.ls 'dir' do |entry|
    puts entry
    end

    entries = Dir.ls 'dir'
    p entries


    harp:~ > ruby a.rb
    dir/a
    dir/b
    dir/c
    ["dir/a", "dir/b", "dir/c"]

    -a
    --
    be kind whenever possible... it is always possible.
    - the dalai lama
     
    , May 4, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lawrence
    Replies:
    2
    Views:
    10,126
    Andrew Thompson
    Nov 20, 2004
  2. mrmagoo
    Replies:
    6
    Views:
    9,314
    Revathishre
    May 6, 2011
  3. Roedy Green
    Replies:
    3
    Views:
    437
    Mike Schilling
    Sep 13, 2008
  4. Ajay Garg
    Replies:
    5
    Views:
    210
  5. Isaac Won
    Replies:
    9
    Views:
    387
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page