Ruby get folder from file path




















Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 4 months ago. Active 2 years, 1 month ago. Viewed 61k times. Programming language: Ruby 1. Improve this question. Nakilon Onetimeposter Onetimeposter 1, 1 1 gold badge 7 7 silver badges 5 5 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. You can use the method sysread to read the contents of a file.

You can open the file in any of the modes when using the method sysread. This statement will output the first 20 characters of the file. The file pointer will now be placed at the 21st character in the file. You can use the method syswrite to write the contents into a file. You need to open the file in write mode when using the method syswrite. This method belongs to the class File. The class File is a subclass of the class IO. The class IO also has some methods, which can be used to manipulate files.

One of the IO class methods is IO. This method returns the contents of the file line by line. The following code displays the use of the method IO. In this code, the variable arr is an array. Each line of the file input. Therefore, arr[0] will contain the first line, whereas arr[1] will contain the second line of the file. This method also returns output line by line. The difference between the method foreach and the method readlines is that the method foreach is associated with a block.

However, unlike the method readlines , the method foreach does not return an array. This code will pass the contents of the file test line by line to the variable block, and then the output will be displayed on the screen. You can rename and delete files programmatically with Ruby with the rename and delete methods. Following is the example to change mode of an existing file test.

All files are contained within various directories, and Ruby has no problem handling these too. Whereas the File class handles files, directories are handled with the Dir class.

To change directory within a Ruby program, use Dir. You can get a list of the files and directories within a specific directory using Dir. Returns false on Windows. Equivalent to File::chmod , but does not follow symbolic links so it will change the permissions associated with the link, not the file referenced by the link.

Often not available. Equivalent to File::chown , but does not follow symbolic links so it will change the owner associated with the link, not the file referenced by the link. Returns number of files in the argument list.

Creates a new name for an existing file using a hard link. Not available on all platforms. Same as File::stat , but does not follow the last symbolic link. Instead, reports on the link itself. Sets the access and modification times of each named file to the first two arguments.

If a file is a symlink, this method acts upon the link itself as opposed to its referent; for the inverse behavior, see File. Returns the number of file names in the argument list.

Returns the modification time for the named file as a Time object. Opens the file named by filename according to the given mode and returns a new File object.

See IO. If a file is being created, permission bits may be given in perm. These mode and permission bits are platform dependent; on Unix systems, see open 2 and chmod 2 man pages for details. The new File object is buffered mode or non-sync mode , unless filename is a tty. With no associated block, File. If the optional code block is given, it will be passed the opened file as an argument and the File object will automatically be closed when the block terminates.

The value of the block will be returned from File. If a file is being created, its initial permissions may be set using the perm parameter. See File. Returns true if the named file exists and the effective used id of the calling process is the owner of the file. Returns true if the named file is readable by the effective user and group id of this process. Returns true if the named file is readable by the real user and group id of this process.

Returns the real absolute pathname of pathname in the actual filesystem. The real pathname doesn't contain symlinks or useless dots. Returns the real absolute pathname of pathname in the actual filesystem not containing symlinks or useless dots. Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed. Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname and File::basename.

Raises a NotImplemented exception on platforms that do not support symbolic links. Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions, so a umask of would make a file read-only for everyone.

If a file is a symlink, this method acts upon its referent rather than the link itself; for the inverse behavior see File. Returns nil otherwise. The meaning of the bits is platform dependent; on Unix systems, see stat 2. Returns true if the named file is writable by the effective user and group id of this process.

Returns true if the named file is writable by the real user and group id of this process. Returns the last access time a Time object for file , or epoch if file has not been accessed. Actual effects are platform dependent; on Unix systems, see chmod 2 for details. Follows symbolic links. Also see File lchmod.



0コメント

  • 1000 / 1000