From: Sean O'Halpin Date: 2012-08-28T06:52:57+09:00 Subject: Re: Parsing unix filenames from STDIN On Mon, Aug 27, 2012 at 10:51 AM, Robert Klemme wrote: > > Two additional cents: > > 1. I'd rather use $stdin instead of STDIN, see here for reasoning: > http://stackoverflow.com/questions/4279604/what-is-the-difference-between-stdin-and-stdin-in-ruby Good point. I agree. > > 2. One can as well use the usual iteration for reading lines from files. > > => > > require 'shellwords' > > $stdin.each_line do |line| > filename = Shellwords.shellwords(line).join('') > p filename > # expand wildcards > p Dir[filename] > end > > Advantage is also that the scope of "line" is limited to the block > reducing potential for errors. Another good point. > Btw, Sean, are you sure David will join all the shellwords? No, I wrote my response after quickly reading his examples, not by reading carefully to the end of his post. > I am > asking since he said there were commands as well and he basically > wants to parse in the same way the shell does. So: > > irb(main):006:0> ['my\\ file.txt', '"my file.txt"', 'ls foo\\ > bar.txt'].each {|s| printf "%p -> %p\n", s, Shellwords.shellwords(s)} > "my\\ file.txt" -> ["my file.txt"] > "\"my file.txt\"" -> ["my file.txt"] > "ls foo\\ bar.txt" -> ["ls", "foo bar.txt"] > => ["my\\ file.txt", "\"my file.txt\"", "ls foo\\ bar.txt"] > Hat trick! I agree with all your points :) > > Kind regards > > robert > > -- > remember.guy do |as, often| as.you_can - without end Indeed. All the best, Sean