Ruby: File/IO

Posted on March 6, 2008. Filed under: Uncategorized |

The 2 most important classes for File IO are File and Dir .This has a lot of class methods which can be accessed as File:: or File.method_name.There is another class calle FileUtil which also has a lot of useful methods.2 most important global variable you must know when using File/Dir is ARGV / ARGF .ARGV is argument variable array which is supplied with the program.ARGF is file stream which is defined with what ever file name that is available in ARGV.If there are more than one in ARGV then one by one it will be opened.

ARGV << “filename”

print while gets ———–> will print the entire file to the screen

gets would get from the file in ARGF which in turn take the value from ARGV(Argument Variable , which is the argument passed to the program) .and the get would put the read content to $_ .print would take the content from $_ and put it to the screen.print/gets is the kernel methods which has it IO devices.for print it is the screen and gets is from ARGF otherwise it is from keyword…

Common Dir Method : Associated with directory would be

mkdir,rmdir,chdir,pwd,getwd,entries(would give all the elements in that directory.It has a block code as well).or you can use Dir.foreach

Instance_methods:you can open the directory stream and start playing with it. Dir.open(“file path”).

after which you check the path d.path d.tell -> what are you processing(which entry) now? d.read ->next entry and d.rewind goes to the start of the directory. d.close will close the stream.

File…except for outer operation everything is instance_methods…

File.open,File.delete,File.rename,File.exists?,File.readble?,File.executable?,File.directory?,File.size,File.writable?,File.zero?(zero size),File.ftype,File.ctime,File.mtime,File.atime(last accessed),

The ftype method identifies the type of the file by returning one of the following:
file, directory, characterSpecial, blockSpecial, fifo, link, socket, or unknown.

File.new : type of file action…

“r” Read-only. Starts at beginning of file (default mode).
“r+” Read-write. Starts at beginning of file.
“w” Write-only. Truncates existing file to zero length or creates a new file for writing.
“w+” Read-write. Truncates existing file to zero length or creates a new file for reading and writing.
“a” Write-only. Starts at end of file if file exists; otherwise, creates a new file for writing.
“a+” Read-write. Starts at end of file if file exists; otherwise, creates a new file for reading and writing.
“b” (DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above.

open,each,close : all part of the IO Class………..not part of File…(only file related activities come as part of file )

file.each(each line),file.lineno,file.closed?,file.close,file.chmod,file.chown.

The best way to read a file File.open(“testfile”,”r”) do|f|

f.each_line(line_separator){|line| print line }

#process File

end

This would automatically take care of closing the file…

reading / writing image files.

File.open(“tgt.gif”,”wb”) do|f|

File.open(“src.gif”,”rb”) do|f1|

f.print f1.read

end

end

f.read(reads the entire file in on read),f.readline(one line),f.readlines(read all lines into a array) or use each_line for a block effect.

combine open with each_line to get foreach

IO.foreach(“st.rb”){|line| puts line } || IO.read(“st.rb”),IO.readlines(“st.rb”)

IO Classes: all Input/Output operation is performed using this class and is duplex .There are 3 global variables defined for you $stdin,$stdout,$stderr of type IO.File is the only Standard subclass of IO.meaning it can do operation just like any IO.

<< write into the buffer of the stream .YOu can call the flush on the object to push it after sometime.fileno will tell you if it is 0,1,2 (in,out,err)

 

2 important subclass of IO are File / BasicSocket

and use require “open-uri”

using open kernel method to connect to any http site…

open(http://www.google.com) do |f|

f.read.scan(/<img src=”(.*?)”>/m).uniq

end

Make a Comment

Make a Comment: ( None so far )

blockquote and a tags work here.

  •  

    March 2008
    M T W T F S S
    « Feb   Apr »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • My Fav

Liked it here?
Why not try sites on the blogroll...