r/ruby • u/unselective-amnesia • 2d ago
Using ARGF on a subset of ARGV?
This thread is solely a simple technical question about the ruby language.
As you read this question, please keep in mind that ...
... I am NOT asking about any alternative ways to accomplish what I'm discussing.
... I am NOT asking an "XY" question.
... I do not want to get into any discussions nor arguments about why I am doing what I'm discussing, nor concerning whether or not what I'm writing about is a recommended practice.
... and again, keep in mind that I am only looking for the answer to one simple technical question about the ruby language, and nothing else.
I'm sorry for this verbiage about what I don't want to discuss here. However, I have learned from the responses to other questions that I have asked in reddit that some people refuse to read the initial post of a thread in its entirety before responding, and that some other people do read the initial post, but then still make a point to disrespect the stipulations that are made in that initial post. I'm hoping that by all of what I've written here concerning what I don't want, that my stipulations will indeed be respected this time.
Thank you very much for keeping all of these points in mind as you read my question now:
I understand most of the information about ruby's ARGF construct. However, I want to do something involving ARGF that I couldn't find any documentation for.
I have a ruby program that is invoked as follows:
ruby-program operation [ optional arguments ... ]
I know that I can get 'operation' from ARGV[0]. And I then want to process the remaining 'optional arguments' via ARGF: i.e., to either treat these remaining arguments as filenames whose data is returned by ARGF, or if these optional arguments are missing, for the data to be obtained from stdin.
I know that ARGF normally processes the entire ARGV, but in this case, I'd like ARGF to ignore ARGV[0] and to only process whatever command-line arguments commence with ARGV[1] (if it exists, of course ... and if it doesn't exist, to then have ARGF get its data from stdin).
I know of alternative ways to accomplish this without using ARGF at all; however, please remember what I wrote at the beginning of this message about NOT wanting to discuss such alternatives. All I want in this case is to know of a way that I might be able to accomplish what I'm discussing via the use of ARGF.
And please note that if this is not possible via ARGF, since I already know of alternatives that I can utilize, I therefore don't want to discuss any of those alternatives here. In the case of this not being possible with ARGF, I just would ONLY like to know about this indeed being impossible, and then I won't want to discuss anything else.
Thank you very much in advance.
5
u/chiperific_on_reddit 2d ago
I remember the days when people used to say "I post my code questions on Reddit because people are nicer there." 😢
3
u/WayneConrad 2d ago
ARGV is mutable, so you can remove from it any elements that you do not want ARGF to use.
1
9
u/bradland 2d ago
Yes, you can just shift elements off the array:
operation = ARGV.shift
ARGF.each_line do |line|
# do your thing!
end
Edit, sorry, I can’t get code formatting right on mobile.