From d00fe54803361cb0a63325427f91a745a2488b72 Mon Sep 17 00:00:00 2001 From: knu Date: Thu, 12 Jan 2012 18:49:30 +0000 Subject: [PATCH] * lib/shellwords.rb (Shellwords#shellescape): shellescape() now stringifies the given object using to_s. * lib/shellwords.rb (Shellwords#shelljoin): shelljoin() accepts non-string objects in the given array, each of which is stringified using to_s. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34287 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ NEWS | 5 +++++ lib/shellwords.rb | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index c87146519c..42548dc844 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ Fri Jan 13 03:46:53 2012 Akinori MUSHA + * lib/shellwords.rb (Shellwords#shellescape): shellescape() now + stringifies the given object using to_s. + + * lib/shellwords.rb (Shellwords#shelljoin): shelljoin() accepts + non-string objects in the given array, each of which is + stringified using to_s. + * lib/shellwords.rb: Fix rdoc markups. Fri Jan 13 03:38:36 2012 Akinori MUSHA diff --git a/NEWS b/NEWS index 58370a6a28..32480e014f 100644 --- a/NEWS +++ b/NEWS @@ -55,6 +55,11 @@ with all sufficient information, see the ChangeLog file. * Resolv::DNS#timeouts= * Resolv::DNS::Config#timeouts= +* shellwords + * Shellwords#shellescape() now stringifies the given object using to_s. + * Shellwords#shelljoin() accepts non-string objects in the given + array, each of which is stringified using to_s. + === Language changes === Compatibility issues (excluding feature bug fixes) diff --git a/lib/shellwords.rb b/lib/shellwords.rb index 550c2693de..2aaa30c23c 100644 --- a/lib/shellwords.rb +++ b/lib/shellwords.rb @@ -57,7 +57,8 @@ module Shellwords end # Escapes a string so that it can be safely used in a Bourne shell - # command line. + # command line. +str+ can be a non-string object that responds to + # +to_s+. # # Note that a resulted string should be used unquoted and is not # intended for use in double quotes nor in single quotes. @@ -77,6 +78,8 @@ module Shellwords # Multibyte characters are treated as multibyte characters, not # bytes. def shellescape(str) + str = str.to_s + # An empty argument will be skipped, so return empty quotes. return "''" if str.empty? @@ -101,7 +104,9 @@ module Shellwords end # Builds a command line string from an argument list +array+ joining - # all elements escaped for Bourne shell and separated by a space. + # all elements escaped for Bourne shell into a single string with + # fields separated by a space, where each element is stringified + # using +to_s+. # # open('|' + Shellwords.join(['grep', pattern, *files])) { |pipe| # # ... @@ -113,6 +118,11 @@ module Shellwords # # ... # } # + # It is allowed to mix non-string objects in the elements as allowed + # in Array#join. + # + # output = `#{['ps', '-p', $$].shelljoin}` + # def shelljoin(array) array.map { |arg| shellescape(arg) }.join(' ') end