Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to make a File Creator with Ruby
#1
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. Smile

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:
############################################################################
#    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 "Enter your file name:"
filename = gets.chomp

puts "Enter the Extension of the File (e.g. .txt, .rb):"
ext = gets.chomp

puts "Enter the Directory where you want to put it: [Please remember to use a trailing Slash]"
directory = gets.chomp

puts "So, you want to make the file: " + filename + ext + "? [y/n]"
yesno = gets.chomp

if yesno == "y"
puts "And the save directory is: " + directory

puts "Creating.."
begin
File.new(directory + filename + ext, 'w')
puts "Success!"
rescue => e
puts "---> Failed To create file.\n\t|\n\t+---> #{e.class} #{e.message}"
end
end

#! /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. Smile


Puts and Gets.Chomp
Okay, so in my code you would've seen alot of this:
Code:
puts "Enter your file name:"
filename = gets.chomp

puts "Enter the Extension of the File (e.g. .txt, .rb):"
ext = gets.chomp

puts "Enter the Directory where you want to put it: [Please remember to use a trailing Slash]"
directory = gets.chomp

puts "So, you want to make the file: " + filename + ext + "? [y/n]"
yesno = gets.chomp

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

puts "Hello, World!"
Hello, World!"
Well, that was more of Code + Output Smile

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

puts "Hello there, please enter your Password: "
pass = gets.chomp
puts "Okay, let's test it, please enter your password to get through!: "
passtest = gets.chomp
if passtest == pass
puts "Congratulations, it worked!"
if passtest != pass
puts "Sorry, that didn't seam to work.
With what you've learned from above, hopeufully you should be able to make sense of that.

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.."
begin
File.new(directory + filename + ext, 'w')
puts "Success!"
rescue => e
puts "---> Failed To create file.\n\t|\n\t+---> #{e.class} #{e.message}"
end
end

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."
Smile What'd I tell you, it's a handy thing to do! Smile

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. Big Grin
I hope it was informative and I hope you learn something from it. Smile

Thanks!
-Jordan.
At the top will be the same place you hang from.
Reply
#2
That's way too easy....
Thanks for teaching me man....
Reply
#3
No problem MoTU, anytime! I hope it was easy to understand.. and informative enough? Smile
Glad you enjoyed it. Smile
At the top will be the same place you hang from.
Reply
#4
It was easy to understand and really informative, but I'm now going through the File Creator tut to understand this one more... ;)
Reply
#5
Good luck with it man, and thanks for the kind feedback. If you're having any problems, just ask me. Smile
At the top will be the same place you hang from.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting Started With Ruby eax 7 4,691 12-13-2012, 10:03 AM
Last Post: L-Carnitine
  [Tut] How to install Ruby on Mac, Windows and Linux Jordan L. 13 7,573 12-13-2012, 07:49 AM
Last Post: L-Carnitine
  Ruby eBook sockatobi 7 2,731 12-13-2012, 07:48 AM
Last Post: L-Carnitine
  what is ruby on rails DaUB3R 3 1,822 11-01-2012, 04:20 AM
Last Post: johnnymward
  Should I Start Ruby? BreShiE 13 4,934 11-13-2011, 04:14 PM
Last Post: Speedy

Forum Jump:


Users browsing this thread: 2 Guest(s)