Difference between revisions of "Ruby Directory Handling"
(→Changing Directory in Ruby) |
|||
Line 12: | Line 12: | ||
== Changing Directory in Ruby == | == Changing Directory in Ruby == | ||
<google>ADSDAQBOX_FLOW</google> | <google>ADSDAQBOX_FLOW</google> | ||
− | When a Ruby application is launched it is usually done from a particular directory. Often it is necessary to navigate to a different directory somewhere on the | + | When a Ruby application is launched it is usually done from a particular directory. Often it is necessary to navigate to a different directory somewhere on the file system from within the Ruby code. Ruby provides a number of useful directory navigation methods in the ''Dir'' class. |
− | First, it is often useful to identify the current directory. This can be done with the ''pwd'' | + | First, it is often useful to identify the current directory. This can be done with the ''pwd'' method of the Ruby ''Dir'' class: |
<tt> | <tt> | ||
Line 21: | Line 21: | ||
</tt> | </tt> | ||
− | Changing the current working directory in Ruby is achieved using the ''chdir'' method of the Ruby ''Dir'' class. This | + | Changing the current working directory in Ruby is achieved using the ''chdir'' method of the Ruby ''Dir'' class. This method takes the path of the destination directory as an argument: |
<tt> | <tt> | ||
Line 38: | Line 38: | ||
== Directory Listings in Ruby == | == Directory Listings in Ruby == | ||
− | Once we have navigated to the desired directory it is a common requirement to obtain a listing of files contained within that directory. Such a listing can be obtained using the ''entries'' method. The ''entries'' | + | Once we have navigated to the desired directory it is a common requirement to obtain a listing of files contained within that directory. Such a listing can be obtained using the ''entries'' method. The ''entries'' method takes as an argument the path of the directory for which a listing is required and returns an array containing the names of the files in that directory: |
In the following example we request a listing of the files in the current directory, which is represented by a dot (.). | In the following example we request a listing of the files in the current directory, which is represented by a dot (.). |
Revision as of 15:20, 31 March 2009
Previous | Table of Contents | Next |
Ruby String Conversions | Working with Files in Ruby |
It may have escaped your notice, but up until this chapter everything we have done involved working with data in memory. 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.
Changing Directory in Ruby
<google>ADSDAQBOX_FLOW</google> When a Ruby application is launched it is usually done from a particular directory. Often it is necessary to navigate to a different directory somewhere on the file system from within the Ruby code. Ruby provides a number of useful directory navigation methods in the Dir class.
First, it is often useful to identify the current directory. This can be done with the pwd method of the Ruby Dir class:
Dir.pwd => "/home/ruby"
Changing the current working directory in Ruby is achieved using the chdir method of the Ruby Dir class. This method takes the path of the destination directory as an argument:
Dir.chdir("/home/ruby/test")
Creating New Directories
Directory creation in Ruby is handled by the mkdir method of the Dir class. This method takes as its argument the path of the new directory. This can either be a full path to the directory, or a relative path based on the current working directory:
Dir.mkdir("/home/ruby/temp") => 0
Directory Listings in Ruby
Once we have navigated to the desired directory it is a common requirement to obtain a listing of files contained within that directory. Such a listing can be obtained using the entries method. The entries method takes as an argument the path of the directory for which a listing is required and returns an array containing the names of the files in that directory:
In the following example we request a listing of the files in the current directory, which is represented by a dot (.).
Dir.entries(".") => ["techotopia_stats.jpg", "toolButton.png", ".", "..", "techotopia_stats_since_start.jpg", "music_728x90_1.jpg", "music_468x60_a.jpg", "Fedora_essentials.jpg"]
We can use some of the techniques covered in Understanding Ruby Arrays to extract the elements from the array:
dirListing.each { |file| puts file } techotopia_stats.jpg toolButton.png . .. techotopia_stats_since_start.jpg music_728x90_1.jpg music_468x60_a.jpg Fedora_essentials.jpg
Alternatively, we can utilize the foreach method of the Dir class to achieve the same result:
Dir.foreach(".") { |file| puts file } techotopia_stats.jpg toolButton_IST.png . .. techotopia_stats_since_start.jpg music_728x90_1.jpg music_468x60_a.jpg Fedora_essentials.jpg
Summary
This chapter has covered the basics of directory handling in Ruby. The next chapter will cover the concepts of Ruby file handling.
Previous | Table of Contents | Next |
Ruby String Conversions | Working with Files in Ruby |