| | |
command line argument -- ruby
Please support our Ruby advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 78
Reputation:
Solved Threads: 1
i'm a java c/c++ person. trying out ruby for the 1st time!
how do i do something like this in ruby:
note that this code may not compile i pulled it off of a java program i did a while back.
drjay
how do i do something like this in ruby:
Ruby Syntax (Toggle Plain Text)
//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
•
•
Join Date: Nov 2008
Posts: 78
Reputation:
Solved Threads: 1
well i figured it out btw. so for anyone in the future looking to do something similar to the java code above:
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!
Ruby Syntax (Toggle Plain Text)
#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!
Guess I'm late for the party but a simpler way of doing it would be:
ruby Syntax (Toggle Plain Text)
require 'fileutils' FileUtils.copy_file(ARGV[0], ARGV[1])
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Passing command line argument to function (C++)
- Trouble Using Command Line Argument (C)
- Command-line argument syntax (C++)
- What is the Command-Line Argument for??? (C++)
- command line arguments problem. (Shell Scripting)
Other Threads in the Ruby Forum
- Previous Thread: resource for ruby
- Next Thread: Help Handling CSV Columns
| Thread Tools | Search this Thread |






