Extensions to String class:
- shellquote(!): escape non-identifier characters
- stripquotes(!): remove a layer of matching quotes
Backslash all non-identifier chars in string.
# File util/string.rb, line 48
def shellquote()
gsub(/\W/) { |s| "\\"+s }
end
# File util/string.rb, line 52
def shellquote!()
replace(shellquote())
end
Remove a layer of single or double quotes if not backslashed.
# File util/string.rb, line 57
def stripquotes()
# 2 "'s for editors that string-close
if }^(["'"])(.*)(\1)$} =~ self
$2
elsif }^\\(["'"])(.*)(\1)$} =~ self
$1 + $2 + $3
else
self
end
end
# File util/string.rb, line 68
def stripquotes!()
replace(stripquotes(q))
end