Module: Shellwords

Defined in:
lib/ruber/utils.rb

Class Method Summary collapse

Class Method Details

.split_with_quotes(str) ⇒ <String>

Similar to @Shellwords.split@, but attempts to include quotes in the returned array for strings containing spaces.

Since it’s impossible to find out from the output of @Shellwords#split@ whether a string was quoted with signle or double quotes, the following approach is taken: if the string contains double quotes, it’s sourrounded with single quotes; otherwise double quotes are used.

TODO: improve on the above algorithm. whitespaces quoted according to the above algorithm

Parameters:

  • str (String)

    the string to split

Returns:

  • (<String>)

    an array as with @Shellwords#split@ but with strings containing



445
446
447
448
449
450
451
452
453
454
455
# File 'lib/ruber/utils.rb', line 445

def self.split_with_quotes str
  res = split str
  res.map! do |s|
    unless s.include? ' ' then s
    else
      quote = s.include?('"') ? "'" : '"'
      quote + s + quote
    end
  end
  res
end