[Tut] How to make a File Creator with Ruby - 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] How to make a File Creator with Ruby (/showthread.php?tid=4315) |
[Tut] How to make a File Creator with Ruby - Jordan L. - 01-13-2010 Hi there. First off, I'd just like to state that I've only just started learning ruby, and I'm not extremely good or anywhere near that yet. But I hope that by posting some more tuts and what not with Ruby, you'll start to learn so we can all teach each other new things. Okay, so today I'm going to show you how to make a "File Creator" with Ruby. It's just a simple script that lets you make a .txt .rb .html .whatever-the-hell you want. I know it's an easy thing to do manually in most Operating System's, but this is a good script purely for practice. First off, here's my Code I've worked on, It's fully tested and working. I'll break it down into small section throughout this Tutorial, and try to explain it the best I can. Code: ############################################################################ #! /usr/bin/env ruby This tells your script to run of the Ruby Environment. There are alot of other ways to do this, such as "#! /usr/bin/ruby", and others depending on what the result of the command "which ruby" in your terminal / cmd after installing Ruby. I like to go with this command, because even if someone's Ruby isn't installed into the same directory as yours, it should still work. Puts and Gets.Chomp Okay, so in my code you would've seen alot of this: Code: puts "Enter your file name:" Well, the command "puts" is like the print command. By typing either the Puts command or the Print command, you can make the text output onto the terminal or cmd screen. Here is a simple Hello World! Script, showing the puts: Code: #! /usr/bin/env ruby The part of the code saying gets.chomp is an "input system" where the user actually writes into the program. It's a way of storing something using a name, much like a database holds tables. An easy example of this, is storing something for a Password. Say the code is: Code: #! /usr/bin/env ruby Error Handeling and File Creating Okay, this brings us to our final part of the Tutorial. This is a really handy trick that my mate taught me, it's errorhandeling. At the bottom of my code, you would've seen: Code: puts "Creating.." Well, even though it looks complicated, it's actually quite simple. The Error Handeling code, you start before you are making the program do what it's supposed to. See in the above code, I've started it before I wanted to make the file creator. It's stated with the "BEGIN" statement. The Rescue => e is telling the program that if something fails, to fall back to the "puts "---> Failed to create file." command. This command prints out clearly that creating the file did not work, and it will print the exact Error Message and tell you what did not go to plans aswell. Say someone tried to make a file in /root/, it would've let you. It would say "Failed to create file ---> You do not have permission to create something here." What'd I tell you, it's a handy thing to do! Now, you would've seen this code: Code: File.new(directory + filename + ext, 'w') This is the actual code for making the file. As I explained above in the gets.chomp section, where it says "directory + filename + ext" is where it will be saved to, what it's called, and the extension of the file. All of which the users chooses earlier from the gets.chomp commans to the questions. File.new just makes the actual file. Thanks for reading my First Ruby Tut. I hope it was informative and I hope you learn something from it. Thanks! -Jordan. RE: [Tut] How to make a File Creator with Ruby - Gaijin - 01-14-2010 That's way too easy.... Thanks for teaching me man.... RE: [Tut] How to make a File Creator with Ruby - Jordan L. - 01-14-2010 No problem MoTU, anytime! I hope it was easy to understand.. and informative enough? Glad you enjoyed it. RE: [Tut] How to make a File Creator with Ruby - Gaijin - 01-14-2010 It was easy to understand and really informative, but I'm now going through the File Creator tut to understand this one more... ;) RE: [Tut] How to make a File Creator with Ruby - Jordan L. - 01-14-2010 Good luck with it man, and thanks for the kind feedback. If you're having any problems, just ask me. |