Collectives on Stack Overflow. Learn more. Getting a list of folders in a directory Ask Question. Asked 12 years, 1 month ago. Active 1 year, 8 months ago. Viewed 66k times. How do I get a list of the folders that exist in a certain directory with ruby? Nietzche-jou Add a comment. Active Oldest Votes. I've found this more useful and easy to use: Dir. The Dir. Emiliano Poggi Emiliano Poggi Simple and best. Thank you — Biswajit Das. Excellent answer. In Files dir.
Parent Object. Included Modules Enumerable. Files History. Dir Objects of class Dir are directory streams representing directories in the underlying file system. Class Dir: Inherits from class Object.
Returns whether the given path is an empty directory. Returns whether the given path is a directory. Public Class Methods Dir[ string [, string Equivalent to calling Dir. File dir. If no block is given, an enumerator is returned instead.
Returns true if the named file is a directory, false otherwise. Got config. Returns the path to the current working directory of this process as a string. Matches any one character. Examples: Dir [ "config.? Returns the home directory of the current user or the named user if given. Returns a new directory object for the named directory. Returns the file descriptor used in dir. Return a string describing this Dir object.
Returns the path parameter passed to dir 's constructor. Repositions dir to the first entry. See also File. The directory used in these examples contains the two regular files config. Changes the current working directory of the process to the given string.
If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits. The return value of chdir is the value of the block.
Will raise a SystemCallError if the named directory doesn't exist. The optional encoding keyword argument specifies the encoding of the directory. If not specified, the filesystem encoding is used. Changes this process's idea of the file system root. Only a privileged process may make this call. Not available on all platforms. On Unix systems, see chroot 2 for more information.
Deletes the named directory. Raises a subclass of SystemCallError if the directory isn't empty. Returns true if the named file is an empty directory, false if it is not a directory or non-empty. Returns an array containing all of the filenames in the given directory. Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block. Expands pattern , which is a pattern string or an Array of pattern strings, and returns an array containing the matching filenames.
In the previous chapters, you have assigned values to variables and then printed the output using puts statement. The puts statement instructs the program to display the value stored in the variable.
This will add a new line at the end of each line it writes. The following code shows you how to use the gets statement. This code will prompt the user to enter a value, which will be stored in a variable val and finally will be printed on STDOUT. Unlike the puts statement, which outputs the entire string onto the screen, the putc statement can be used to output one character at a time. The print statement is similar to the puts statement.
The only difference is that the puts statement goes to the next line after printing the contents, whereas with the print statement the cursor is positioned on the same line. Until now, you have been reading and writing to the standard input and output. Now, we will see how to play with actual data files. You can create a File object using File. Finally, you can use File. You can use File. However, there is one difference in between File.
The difference is that the File. Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode. Write-only mode.
0コメント