Iterate through every file in one directory

前端 未结 8 2044
忘了有多久
忘了有多久 2020-11-30 16:52

How do I write a loop in ruby so that I can execute a block of code on each file?

I\'m new to ruby, and I\'ve concluded that the way to do this is a do each loop.

8条回答
  •  盖世英雄少女心
    2020-11-30 17:13

    To skip . & .., you can use Dir::each_child.

    Dir.each_child('/path/to/dir') do |filename|
      puts filename
    end
    

    Dir::children returns an array of the filenames.

提交回复
热议问题