views:

211

answers:

2

I want to get all file names from a folder using Ruby.

+2  A: 
Dir.entries(folder)

example:

Dir.entries(".")

Source: http://ruby-doc.org/core/classes/Dir.html#M002303

Željko Filipin
You posted this answer 15 minutes ago. You posted the question 15 minutes ago. Are you experiencing an interesting mental event? Did the right half of your brain not know the answer as your hands were typing the question? Did the part of your mind that did know the answer take over immediately after you finished typing the question? (This seems to be a pattern with your Ruby questions: post question; answer own question right after.)
Telemachus
Looks like he's using SO to document the answers to questions he's just asked. A sort of memo, I suppose. Can't see much wrong with that - after all, even though this one is a little incomplete (`Dir#glob` could have perhaps been mentioned, for instance) there's nothing to prevent someone else from posting a Really Good Answer.'course, I'm mostly a "glass half full" sort of a guy...
Mike Woodhouse
@Mike: In the grand scheme of things, it's probably not a big deal. And as you say if the questions and answers were good, it could be a plus overall for the site. But here both question and answer are so minimal that it doesn't seem especially useful.
Telemachus
@Telemachus I use `Dir` rarely, and every time I need it I have to read documentation. I have posted my question and answer here so I could find it later, and maybe even help someone with the same question. I think I have heard at SO podcast that there is nothing wrong with such behavior. If you have a better answer, please post it. I have posted what I know, I am not a Ruby ninja. I regularly accept answers with the most votes.
Željko Filipin
+2  A: 

You also have the shortcut option of

Dir["/path/to/search/*"]

and if you want to find all Ruby files in any folder or sub-folder:

Dir["/path/to/search/**/*.rb"]
Ian
Or you can do the same with Dir::glob()
Yoann Le Touche
Thanks a lot, I am using Dir[] right now. :)
Željko Filipin