command line argument -- ruby

Thread Solved

Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

command line argument -- ruby

 
0
  #1
May 26th, 2009
i'm a java c/c++ person. trying out ruby for the 1st time!

how do i do something like this in ruby:

  1. //this is java
  2. public static void main ( String args [] ) {
  3. File inFile = new File ( args [ 0 ] );
  4. File outFile = new File ( args [ 1 ] );
  5. DataInputStream reader = new DataInputStream ( new BufferedInputStream ( new FileInputStream ( inFile ) ) );
  6. BufferedWriter writer = new BufferedWriter ( new FileWriter ( outFile ) );
  7. String str;
  8. while (str = reader.readline() != null){
  9. writer.write (str);
  10. }
  11. }

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

drjay
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 78
Reputation: drjay1627 is an unknown quantity at this point 
Solved Threads: 1
drjay1627 drjay1627 is offline Offline
Junior Poster in Training

Re: command line argument -- ruby

 
0
  #2
Jun 3rd, 2009
well i figured it out btw. so for anyone in the future looking to do something similar to the java code above:

  1. #arguments
  2. inFile = ARGV[0]
  3. outFile = ARGV[1]
  4.  
  5. def foo(infile, outfile)
  6. begin
  7. puts "Opening input file..."
  8. fin = File.open(inFile, "r")
  9. puts "Creating output file..."
  10. fout = File.open(outFile, "w")
  11. puts "Reading from input file and writing to output file..."
  12. str = ins.readlines()
  13. fout.puts str
  14. puts "Closing files..."
  15. fin.close
  16. fout.close
  17. puts "Process complete!"
  18. rescue => err
  19. puts "Exception: #{err}"
  20. err
  21. end
  22. end
  23.  
  24. 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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: command line argument -- ruby

 
0
  #3
Jun 12th, 2009
Guess I'm late for the party but a simpler way of doing it would be:
  1. require 'fileutils'
  2. FileUtils.copy_file(ARGV[0], ARGV[1])
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC