From: Lars Christensen Date: 2001-07-30T01:05:39+09:00 Subject: [ruby-talk:18749] Comment support in shellwords.rb This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. Send mail to mime@docserver.cac.washington.edu for more info. ---559023410-851401618-996422655=:79 Content-Type: TEXT/PLAIN; charset=US-ASCII Hi Rubyists. I propose this little cleanup/improvement of shellwords.rb: the patch below makes shellwords.rb ignore shell like comments (# ...), which can be escaped or quoted as the attached test case verifies. Futhermore, it moves the shellwords method to the String class. "require 'shellwords'; include Shellwords" is not particular clean IMO. Why it's own module when it is really an advanced version of String#split? --- /arch/ruby/ruby-1.6.4/lib/shellwords.rb Mon Jun 4 10:59:56 2001 +++ shellwords.rb Sun Jul 29 18:02:33 2001 @@ -8,19 +8,17 @@ # or # # require 'shellwords' -# include Shellwords -# words = shellwords(line) +# words = line.shellwords -module Shellwords - def shellwords(line) - unless line.kind_of?(String) - raise ArgumentError, "Argument must be String class object." - end +class String + def shellwords + line = dup line = line.sub(/\A\s+/, '') words = [] while line != '' field = '' while true + line.sub!(/\A\s*#.*$/, '') if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then #" snippet = $1 snippet.gsub!(/\\(.)/, '\1') @@ -33,7 +31,7 @@ raise ArgumentError, "Unmatched single quote: #{line}" elsif line.sub!(/\A\\(.)/, '') then snippet = $1 - elsif line.sub!(/\A([^\s\\'"]+)/, '') then #' + elsif line.sub!(/\A([^\s\\'"#]+)/, '') then #' snippet = $1 else line.sub!(/\A\s+/, '') @@ -45,5 +43,4 @@ end words end - module_function :shellwords end -- Lars Christensen, larsch@cs.auc.dk ---559023410-851401618-996422655=:79 Content-Type: TEXT/PLAIN; charset=US-ASCII; name="shellwords_test.rb" Content-Transfer-Encoding: BASE64 Content-ID: Content-Description: Content-Disposition: attachment; filename="shellwords_test.rb" cmVxdWlyZSAnLi9zaGVsbHdvcmRzJw0KcmVxdWlyZSAncnVuaXQvdGVzdGNh c2UnDQoNCmNsYXNzIFNoZWxsd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