Iterate through every file in one directory

前端 未结 8 2036
忘了有多久
忘了有多久 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

    I like this one, that hasn't been mentioned above.

    require 'pathname'
    
    Pathname.new('/my/dir').children.each do |path|
        puts path
    end
    

    The benefit is that you get a Pathname object instead of a string, that you can do useful stuff with and traverse further.

提交回复
热议问题