01-13-2010, 11:01 PM
Well, with the opening of the Ruby Section (Finally! ) 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:
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.
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.
In the above code, you will see a line that has:
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.
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:
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.
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..."
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|
Code:
f.puts texttoadd
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 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.