Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Making a Write-To-File Script
#1
Well, with the opening of the Ruby Section (Finally! Yeye) I thought I'd help out and post some tuts and what not. I hope you enjoy. This ones a script on writing something to a file.

First off; here is the finished code which I will then break down into parts and explain to the best I can:
Code:
############################################################################
#    Copyright (C) 2009 by Pyrite Software Developers                                                  
#                                                                                                                                
#                                                                                                                                
#    This program is free software; you can redistribute it and#or modify                        
#    it under the terms of the GNU General Public License as published by                    
#    the Free Software Foundation; either version 2 of the License, or                            
#    (at your option) any later version.                                                                          
#                                                                                                                              
#    This program is distributed in the hope that it will be useful,                                  
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        
#    GNU General Public License for more details.                          
#                                                                          
#    You should have received a copy of the GNU General Public License    
#    along with this program; if not, write to the                        
#    Free Software Foundation, Inc.,                                      
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            
############################################################################

#! /usr/bin/env ruby

puts "Write the Path to the file you would like add to: "
writetofile = gets.chomp

puts "Enter the text you would like to add to the file: "
texttoadd = gets.chomp

puts "Writing..."
begin
open(writetofile, 'a') { |f|
  f.puts texttoadd
}

puts "Success! Your text was successfully added to the file."
rescue => e
puts "---> Failed To read file.\n\t|\n\t+---> #{e.class} #{e.message}"
end

1. The Basics
Well, as in my other tutorial, I explained all about the gets command and the puts command. So I don't feel as if I should continue and say it over again and again then boring you. Here's just a quick brief of it though, for all the newbies to Ruby.
Code:
puts "Write the Path to the file you would like add to: "
writetofile = gets.chomp

puts "Enter the text you would like to add to the file: "
texttoadd = gets.chomp

puts "Writing..."
Writing the puts command will Output something onto the CMD / terminal, while the gets command will let the User Input something. The .chomp command is just handy for removing any whitespace and what not.

Gets is good for getting information, such as a directory they want to install it to / read off, or for saving passwords etc. (All explained in the File Maker tut).

2. Error Handling and Writing to a File.
Code:
begin
open(writetofile, 'a') { |f|
  f.puts texttoadd
}

puts "Success! Your text was successfully added to the file."
rescue => e
puts "---> Failed To read file.\n\t|\n\t+---> #{e.class} #{e.message}"
end

In the above code, you will see a line that has:
Code:
open(writetofile, 'a') { |f|
This tells the program to open the file that the user has input with the above gets code. While the 'a' command is opening it and appending, if I was to write 'w' instead for example, it would mean 'write'. Which in this case we don't want to do that, because using the 'w' command also deletes all other data already in there.

Code:
f.puts texttoadd
This tells your program to input into the file, the 'texttoadd' which is defined as a gets that the User has also input (From the above questions in the code.)

And last but not least, there is the error handling:
Code:
begin
rescue => e
puts "---> Failed To read file.\n\t|\n\t+---> #{e.class} #{e.message}"
end
This tell the program that if for any reason it can't write to the file, to fall back and give the error message. (More is explained in the other tutorial too.)


This is just a handy little script for adding contents to files, it can come in use to alot of people. Feel free to try out the program, but I ask you not to remove the copyright.

Have fun and keep learning Ruby!
Thanks!
-Jordan.
At the top will be the same place you hang from.
Reply
#2
LOL... Awsome...
It's so easy.. Thanks for teaching man...
May I ask you how to delete the file?

Unimportant Edit (Click to View)
Reply
#3
(01-14-2010, 03:02 AM)Master of The Universe Wrote: LOL... Awsome...
It's so easy.. Thanks for teaching man...
May I ask you how to delete the file?

Unimportant Edit (Click to View)

Congrats on the 900th!
And I'm glad you're learning Smile

As for deleting the file, how do you mean? Do you mean actually delete the file?

For linux:
Code:
sudo rm rf /blah/blah/file.txt

For Windows:
Code:
del C:\Users\blah\file.txt

I'm not sure for Mac though.

Or do you mean deleting what's inside the folder?
At the top will be the same place you hang from.
Reply
#4
Nah I mean to delete it with Ruby...
Reply
#5
Umm.. I'm not completely sure, but I think it's:
Code:
File.delete("directory to file here")

Try that and see, tell me if you have a problem.
At the top will be the same place you hang from.
Reply
#6
(01-14-2010, 03:13 AM)trilobyte- Wrote: Umm.. I'm not completely sure, but I think it's:
Code:
File.delete("directory to file here")

Try that and see, tell me if you have a problem.

HaHaha... For real, having common sense doesn't mean using it.....
Thanks man, works fine!
Reply
#7
Nice tutorial dude.. Look forward to seeing some more Smile
FREE PSN CARDS, XBOX LIVE, GAMES + MORE VIEW THREAD Here Yeye
Reply
#8
Thanks Codine, thanks for the feedback. Smile
At the top will be the same place you hang from.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Printing the File Contents Jordan L. 8 2,795 12-12-2010, 03:49 PM
Last Post: Distant Relatives
  [Tut] How to make a File Creator with Ruby Jordan L. 4 1,733 01-14-2010, 02:53 AM
Last Post: Jordan L.

Forum Jump:


Users browsing this thread: 1 Guest(s)