Ruby/File handling Input Output

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

[edit] Ruby File Handling.Input and Output

File handling in Ruby is done with the File subclass of IO class.

Creating a new file.The new and open methods

A new file is created by using the "new" method on the File class.

file = File.new("myfile", "rw")
# ... operations to be done on file
file.close

In the code sample above, a file named "myfile" is created with read/write permissions assigned.This will allow you to read from the file or write into it.You should note that in case of a raised exception while processing the file, file.close may not happen as planned, but later along the way,in the meantime holding onto the allocated resources.

Alternatively, you can use the "open" method which is very similar to the "new" one, except:in case a block is associated with the method call, instead of returning the new object, it invokes the block, passing the newly opened File as a parameter.The file is closed when the block exits. In the case of using the "open" method, if an exception is raised inside a block, the file is closed before the exception propagates to the caller.

file = File.open("myfile", "rw")
# ... operations to be done on file
file.close


Reading/writing to files

The gets method

File.open("myfile") do |file|
while content = file.gets
# ... operations on file
end
end

This code reads from the file as long as there is "something" in the file, note the "while content = file.gets" line.

The each_byte method

To be used when you need to process each character in an expression.Here's a sample:

"Sample code".each_byte {|b| puts b.chr}

Image:Each.gif

Obviously, it can be used on files also, to read characters and process sequentially.

File.open("myfile") do |file|
file.each_byte {....}
end

The each_line method

Similar to each_byte, read one line at a time.

Previous
Personal tools