943,900 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Marked Solved
  • Views: 7004
  • Ruby RSS
May 26th, 2009
0

command line argument -- ruby

Expand Post »
i'm a java c/c++ person. trying out ruby for the 1st time!

how do i do something like this in ruby:

Ruby Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Jun 3rd, 2009
0

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:

Ruby Syntax (Toggle Plain Text)
  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!
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
drjay1627 is offline Offline
87 posts
since Nov 2008
Jun 12th, 2009
0

Re: command line argument -- ruby

Guess I'm late for the party but a simpler way of doing it would be:
ruby Syntax (Toggle Plain Text)
  1. require 'fileutils'
  2. FileUtils.copy_file(ARGV[0], ARGV[1])
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: resource for ruby
Next Thread in Ruby Forum Timeline: Help Handling CSV Columns





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC