1. Creating a New File with Ruby
In Ruby, we create new file by using the "new method" of the
File class. The new method takes two arguments: the first
is the name of the file to be created and the second is the mode
in which the file will be opened; such as: "r" for read only, "r+" for
read and write , "w" for write only, "w+" for read and write, and
"b" for Binary File Mode.
Example: creating this_file file in write mode:
(The related files are set in C:\ruby\examples directory.)
>> File.new("this_file.txt","w")
=> #<File:this_file.txt>
2. Opening Existing Files
Opening files is done by using the "open" method of
the Ruby File class. It can be done in different modes.
Example:
Open the file this_file:
>> file =File.open("this_file.txt")
=> #<File:this_file.txt>
Open the file this_file in read and write mode:
>> file =File.open("this_file.txt","r+")
=> #<File:this_file.txt>
3. Check the opening of existing files
To check whether a file is already open, use
the "closed?" method:
>> file.closed?
=> false
4. Closing Existing Files
By using the "close method" of the Ruby File class, we
close files:
>> file.close
=> nil
.. and check:
>> file.closed?
=> true
5. Renaming Files
Use "rename" method:
C:\ruby\examples>cd..
C:\ruby>cd files
C:\ruby\files>irb --simple-prompt
>> File.new("old.txt","w")
=> #<File:old.txt>
>> File.rename("old.txt","new.txt")
=> 0
6. Deleting Files
>> File.delete("new.txt")
=> 1
7. Reading Files
Here, I used "eval":
The contents of the "new_file" file is:
string - is for small data types such as a title.
text - is for longer pieces of textual data, such as the description.
integer - is for whole numbers.
float - is for decimals.
datetime and timestamp - store the date and time into a column.
date and time - store either the date only or time only.
binary - is for storing data such as images, audio, or movies.
boolean - is for storing true or false values.
ruby> @the_file= File.open("new_file.txt")
#<File:new_file.txt>
ruby> @the_file.each {|line| print line }
string - is for small data types such as a title.
text - is for longer pieces of textual data, such as the description.
integer - is for whole numbers.
float - is for decimals.
datetime and timestamp - store the date and time into a column.
date and time - store either the date only or time only.
binary - is for storing data such as images, audio, or movies.
boolean - is for storing true or false values.#<File:new_file.txt>
ruby> @that_file = File.open("new_file.txt","r")
#<File:new_file.txt>
ruby> @that_file.readline
"string - is for small data types such as a title.\n"
ruby> @that_file.readline
"text - is for longer pieces of textual data, such as the description.\n"
ruby> @that_file.readline
"integer - is for whole numbers.\n"
ruby> @that_file.readline
"float - is for decimals.\n"
ruby> @that_file.readline
"datetime and timestamp - store the date and time into a column.\n"
ruby> @that_file.readline
"date and time - store either the date only or time only.\n"
ruby> @that_file.readline
"binary - is for storing data such as images, audio, or movies.\n"
ruby> @that_file.readline
"boolean - is for storing true or false values.\n"
ruby> @that_file.readline
ERR: (eval):1:in `readline': end of file reached
ruby>
8. writing Files
ruby> @this_file = File.new("earth.txt","w+")
#<File:earth.txt>
ruby> @this_file.puts("line 1")
nil
ruby> @this_file.puts("line 2")
nil
ruby> @this_file.puts("line 3")
nil
ruby> @this_file.rewind
0
ruby> @this_file.readline
"line 1\n"
ruby> @this_file.readline
"line 2\n"
ruby>
7. Other Information About Files
Here, I used irb:
>> File.new("new_file.txt","w")
=> #<File:new_file.txt>
>> File.exists?("new_file.txt")
=> true
>> File.exists?("some_file.txt")
=> false
>> File.file?("some_file.txt")
=> false
>> File.directory?("some_file.txt")
=> false
>> File.directory?("files")
=> false
>> File.directory?("/files")
=> false
>> File.directory?("ruby/files")
=> false
>> File.directory?("ruby")
=> false
>> File.directory?("/ruby")
=> true
>> File.readable?("new_file.txt")
=> true
>> File.writable?("new_file.txt")
=> true
>> File.executable?("new_file.txt")
=> true
>> File.size("new_file.txt")
=> 0
(here I filled ou the file)
>> File.size("new_file.txt")
=> 414
>> File.zero?("new_file.txt")
=> false
>> File.ftype("new_file.txt")
=> "file"
>> File.ftype("../files")
=> "directory"
>> File.ctime("new_file.txt")
=> Mon Jun 02 16:45:01 -0400 2008
>> File.mtime("new_file.txt")
=> Mon Jun 02 17:12:31 -0400 2008
>> File.atime("new_file.txt")
=> Mon Jun 02 17:12:31 -0400 2008
|