tags:

views:

75

answers:

2
ruby somescript.rb somehugelonglistoftextforprocessing

is this a bad idea? rather should i create a separate flat file containig the somehugelonglistoftextforprocessing, and let somescript.rb read it ?

does it matter if the script argument is very very long text(1KB~300KB) ? what are some problems that can arise if any.

+6  A: 

As long as the limits of your command-line handling code (e.g., bash or ruby itself) are not exceeded, you should have no technical problems in doing this.

Whether it's a good idea is another matter. Do you really want to have to type in a couple of hundred kilobytes every single time you run your program? Do you want to have to remember to put quotes around your data if it contains spaces?

There are a number of ways I've seen this handled which you may want to consider (this list is by no means exhaustive):

  • Change your code so that, if there's no arguments, read the information from standard input - this will allow you to do either
       ruby somescript.rb myData
    or
       ruby somescript.rb <myFile.txt.

  • Use a special character to indicate file input (I've seen @ used in this way). So,
       ruby somescript.rb myData
    would use the data supplied on the command line whilst
       ruby somescript.rb @myFile.txt
    would get the data from the file.

My advice would be to use the file-based method for that size of data and allow an argument to be used if specified. This covers both possible scenarios:

  • Lots of data, put it in a file so you won't have to retype it every time you want to run your command.
  • Not much data, allow it to be passed as an argument so that you don't have to create a file for something that's easier to type in on the command line.
paxdiablo
+1  A: 

I second paxdiablo's recommendation. There's some good discussion of this approach on another SO thread that might be useful

Patrick Reagan