IT Community - Software Programming, Web Development and Technical Support

Ruby File handling

This is a discussion on Ruby File handling within the Ruby forums, part of the Web Development category; Hi, Could any one explain file handling in Ruby?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 03-28-2008, 11:05 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Question Ruby File handling

Hi,

Could any one explain file handling in Ruby?
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-28-2008, 11:08 PM
nnraja nnraja is offline
D-Web Programmer
 
Join Date: May 2007
Posts: 94
nnraja is on a distinguished road
Default Re: Ruby File handling

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 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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-28-2008, 11:15 PM
satheesh satheesh is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 95
satheesh is on a distinguished road
Default Re: Ruby File handling

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-28-2008, 11:19 PM
bluesky bluesky is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 201
bluesky is on a distinguished road
Default Re: Ruby File handling

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}
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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
File extension of the code file & object repository file in QTP vigneshgets Testing Tools 1 01-16-2008 10:43 PM
Error Handling in PHP sivaramakrishnan PHP Programming 13 09-21-2007 04:03 AM
File Handling in PHP ragavraj PHP Programming 20 09-12-2007 10:46 PM
How can I do error handling in php? itbarota PHP Programming 0 09-11-2007 11:39 PM
Error Handling vigneshgets Software Testing 0 05-18-2007 02:36 AM


All times are GMT -7. The time now is 11:58 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0