[Tut] Making a Write-To-File Script - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Ruby and Ruby on Rails (https://www.supportforums.net/forumdisplay.php?fid=55) +---- Thread: [Tut] Making a Write-To-File Script (/showthread.php?tid=4325) |
[Tut] Making a Write-To-File Script - Jordan L. - 01-13-2010 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: Code: ############################################################################ 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: " 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 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 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. RE: [Tut] Making a Write-To-File Script - Gaijin - 01-14-2010 LOL... Awsome... It's so easy.. Thanks for teaching man... May I ask you how to delete the file? Unimportant Edit (Click to View) RE: [Tut] Making a Write-To-File Script - Jordan L. - 01-14-2010 (01-14-2010, 03:02 AM)Master of The Universe Wrote: LOL... Awsome... Congrats on the 900th! And I'm glad you're learning 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? RE: [Tut] Making a Write-To-File Script - Gaijin - 01-14-2010 Nah I mean to delete it with Ruby... RE: [Tut] Making a Write-To-File Script - Jordan L. - 01-14-2010 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. RE: [Tut] Making a Write-To-File Script - Gaijin - 01-14-2010 (01-14-2010, 03:13 AM)trilobyte- Wrote: Umm.. I'm not completely sure, but I think it's: HaHaha... For real, having common sense doesn't mean using it..... Thanks man, works fine! RE: [Tut] Making a Write-To-File Script - Codine - 01-14-2010 Nice tutorial dude.. Look forward to seeing some more RE: [Tut] Making a Write-To-File Script - Jordan L. - 01-14-2010 Thanks Codine, thanks for the feedback. |