Iterate through every file in one directory

前端 未结 8 2007
忘了有多久
忘了有多久 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:27
    Dir.foreach("/home/mydir") do |fname|
      puts fname
    end
    
    0 讨论(0)
  • 2020-11-30 17:30

    The find library is designed for this task specifically: https://ruby-doc.org/stdlib-2.5.1/libdoc/find/rdoc/Find.html

    require 'find'
    Find.find(path) do |file|
      # process
    end
    

    This is a standard ruby library, so it should be available

    0 讨论(0)
提交回复
热议问题