DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Ruby (http://www.daniweb.com/forums/forum73.html)
-   -   command line argument -- ruby (http://www.daniweb.com/forums/thread194080.html)

drjay1627 May 26th, 2009 5:08 pm
command line argument -- ruby
 
i'm a java c/c++ person. trying out ruby for the 1st time!

how do i do something like this in ruby:

//this is java 
public static void main ( String args [] ) {
        File inFile  = new File ( args [ 0 ] );
        File outFile = new File ( args [ 1 ] );
DataInputStream reader = new DataInputStream ( new BufferedInputStream ( new FileInputStream ( inFile ) ) );
                BufferedWriter writer = new BufferedWriter ( new FileWriter ( outFile ) );
String str;
while (str = reader.readline() != null){
writer.write (str);
}
}

note that this code may not compile i pulled it off of a java program i did a while back.

drjay

drjay1627 Jun 3rd, 2009 1:21 pm
Re: command line argument -- ruby
 
well i figured it out btw. so for anyone in the future looking to do something similar to the java code above:

#arguments
inFile = ARGV[0]
outFile = ARGV[1]

def foo(infile, outfile)
        begin
                puts "Opening input file..."
                fin = File.open(inFile, "r")
                puts "Creating output file..."               
                fout = File.open(outFile, "w")
                puts "Reading from input file and writing to output file..."               
                str = ins.readlines()
                fout.puts str
                puts "Closing files..."
                fin.close
                fout.close
                puts "Process complete!"
        rescue => err
                puts "Exception: #{err}"
                err
        end
end

foo(inFile,  outFile)

takes in 2 files as command line arguments. 1st file being the input file and the second being the output file. then it writes the input to the output file.

happy coding

drjay!

~s.o.s~ Jun 12th, 2009 2:45 pm
Re: command line argument -- ruby
 
Guess I'm late for the party but a simpler way of doing it would be:
require 'fileutils'
FileUtils.copy_file(ARGV[0], ARGV[1])


All times are GMT -4. The time now is 2:58 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC