Coder Profile - Show off your skills, get a coder profile.
 
 
 
code pin board Read/Write Fuction In Ruby Download Source Code
Author Details Code Information
Damner ( Dominik )

Pinned 3 Codes
Posted 1 Coding Articles

Send A Message
View Coders Profile
Language Plain Text
Expires Never
Length 411 Characters (15 Lines)
Password no password
Description

Another Ruby Function
  1. #p027readwrite.rb
  2. # Open and read from a text file
  3. # Note that since a block is given, file will
  4. # automatically be closed when the block terminates
  5. File.open('any.rb', 'r') do |f1|
  6.    while line = f1.gets
  7.      puts line
  8.    end
  9. end
  10.  
  11. # Create a new file and write to it
  12. File.open('test.rb', 'w') do |f2|
  13. # use "\n" for two lines of text
  14. f2.puts "Created by Damner\nCool!"
  15. end
code pin board Back To Code Pin Board Post New Code
Please login to post comments.
 
haskell     Posted 156 Days Ago
 
 
Alternatively:

File.open('[file]', 'r') do |file|
puts file.read
end
 
Cinjection     Posted 180 Days Ago
 
 
I haven't played with file IO, but it looks pretty good.
Page 1 of 1
 
 
Part of the MyPingle Network
Development Blog :: Make A Donation :: Contact Me
Terms & Conditions :: Privacy Policy :: Documents
Version 1.44.00
Copyright © 2007 - 2008, Scott Thompson, All Rights Reserved