background preloader

File & Directory Management

Facebook Twitter

Dir [ruby-doc.org] Dir[ array ] → array click to toggle source Dir[ string [, string ...] ] → array Equivalent to calling Dir.glob(array,0) and Dir.glob([string,...],0). static VALUE dir_s_aref(int argc, VALUE *argv, VALUE obj) { if (argc == 1) { return rb_push_glob(argv[0], 0); } return dir_globs(argc, argv, 0); } chdir( [ string] ) → 0 click to toggle source chdir( [ string] ) {| path | block } → anObject Changes the current working directory of the process to the given string.

Dir [ruby-doc.org]

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. Dir.chdir("/var/spool/mail") puts Dir.pwd Dir.chdir("/tmp") do puts Dir.pwd Dir.chdir("/usr") do puts Dir.pwd end puts Dir.pwd end puts Dir.pwd produces: /var/spool/mail /tmp /usr /tmp /var/spool/mail chroot( string ) → 0 click to toggle source Changes this process’s idea of the file system root. Delete( string ) → 0 click to toggle source Deletes the named directory. Dir.entries("testdir") exist? Exists? C* *c. Create a new directory if it's missing. Ruby Directory Handling. From Techotopia Purchase and download the PDF and ePub versions of this Ruby eBook for only $8.99 It may have escaped your notice, but up until this chapter everything we have done involved working with data in memory.

Ruby Directory Handling

Now that we have covered all the basics of the Ruby language, it is time to turn our attention to working with files and directories in Ruby. [edit] Changing Directory in Ruby When a Ruby application is launched it is usually done from a particular directory. First, it is often useful to identify the current directory. Dir.pwd => "/home/ruby" Changing the current working directory in Ruby is achieved using the chdir method of the Ruby Dir class. Dir.chdir("/home/ruby/test") [edit] Creating New Directories Directory creation in Ruby is handled by the mkdir method of the Dir class. Dir.mkdir("/home/ruby/temp") => 0 [edit] Directory Listings in Ruby In the following example we request a listing of the files in the current directory, which is represented by a dot (.).

[edit] Summary. Ruby File I/O, Directories. Ruby provides a whole set of I/O-related methods implemented in the Kernel module.

Ruby File I/O, Directories

All the I/O methods are derived from the class IO. The class IO provides all the basic methods, such as read, write, gets, puts, readline, getc, and printf. This chapter will cover all the basic I/O functions available in Ruby. For more functions, please refer to Ruby Class IO. The puts Statement: In previous chapters, you 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. Example: #! This will produce the following result: This is variable one This is variable two The gets Statement: The gets statement can be used to take any input from the user from standard screen called STDIN.

The following code shows you how to use the gets statement. . #! Enter a value :This is entered value This is entered value The putc Statement: The output of the following code is just the character H: #! The print Statement: #! #! #! #! File Access.