2014-10-27 142 views

回答

5

Dir#entries可能是你在找什麼:

Dir.entries("dir").each { |f| puts f } 
# ⇒ Makefile 
# ⇒ text.doc 
# ⇒ ... 

請注意,結果可能包含...。爲了防止那些在結果的外觀,您可能需要使用Array#reject(creds到@iain,該片段將所有隱藏文件上* nices篩選):執行glob

Dir.entries("dir").reject{ |e| e.start_with? '.' }.each { |f| puts f } 

另一種選擇是Dir#chdir

希望它有幫助。

+0

'entries'需要一個目錄名稱,而不是一個模式。除非有一個具有該名稱的目錄,否則必須刪除'/ *'。 – Stefan 2014-10-27 08:43:20

+0

@Stefan當然,謝謝。 – mudasobwa 2014-10-27 08:44:13

+0

'reject'在使用'entries'時非常有用,例如'Dir.entries( 「DIR」)拒絕{|即| e.start_with? 「」 } .each {'... – iain 2014-10-27 09:54:10