Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File IO?
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mackie




PostPosted: Tue Apr 08, 2008 3:53 pm   Post subject: File IO?

How do you read/write/make new files in Ruby? I have an idea i must know!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Apr 08, 2008 4:17 pm   Post subject: RE:File IO?

Look into the File class.
Ruby:

contents = File::open("myfile.txt", "r").read
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
haskell




PostPosted: Wed Apr 09, 2008 5:09 am   Post subject: Re: File IO?

Ruby:

File.open("enter_file_name", "r+") do |file|
  file.puts("This goes in the file!")
  file.puts("This also goes to the file!")
  lines = file.read  # Enitre file added to lines

  lines.each_line do |l|
    puts l[7] + '  ' +  l[4] + '  ' # Print the 5th and 8th character of each line 
  end
end
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: