The readlines reads all lines from the specified file and returns them in the form of an array. We go through the array with the each method and print the lines to the terminal. In this section, we work with directories.
We have a Dir class to work with directories in Ruby. With the exists? The pwd method prints a current working directory. This is the directory from which we launched the script. The chdir method changes to another directory. Finally, we remove a directory with the rmdir method. This time the exists? In the second example, we retrieve all of a directory's entries, including its files and subdirectories.
We get the array of files and directories of a current directory. The inspect method gives us a more readable representation of the array. The third example works with a home directory. Every user in a computer has a unique directory assigned to him. It is called a home directory. It is a place where he can place his files and create his own hierarchy of directories.
If we do not specify the user name, then a home directory of a current user is returned. The current user is the owner of the script file. Someone, who has launched the script. Ruby has several ways to execute external programs.
We will deal with some of them. In our examples we use well known Linux commands. Readers with Windows or Mac can use commands specific for their systems. The system command executes an external program in a subshell.
The method belongs to the Kernel Ruby module. Here we execute the pwd command using backticks. The command returns the current working directory. Here we get the output of the uptime command, which tells how long a system is running. We can execute a command with the open method. The method belongs to the Kernel module. It creates an IO object connected to the given stream, file, or subprocess.
If we want to connect to a subprocess, we start the path of the open with a pipe character. In the example, we print the outcome of the ls -l head -3 commands. The combination of these two commands returns the first three lines of the ls -l command. By signing up or logging in, you agree to our Terms of service and confirm that you have read our Privacy Policy. Already a member? Go to Log In.
Forgot your password. Not registered yet? Go to Sign Up. I am assuming that you have gone through the introduction section and know how to write and compile Ruby codes.
Yes, it is simple. You just have to write the matter which you want to print on the screen in between " " after puts and it is done. So, 15 was printed on the screen. This part is only to teach you how to know the error if you are getting one. What if we type this :. It will be same as your filename. In our code, the error is in line 1. 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. The Dir.
0コメント