Module: StrQuote::Quote

Included in:
String
Defined in:
lib/str_quote/quote.rb

Instance Method Summary collapse

Instance Method Details

#quote(quote_char = '"', escape_char = '\\') ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/str_quote/quote.rb', line 3

def quote(quote_char = '"', escape_char = '\\')
  unless escape_char.nil?
    e = self.gsub(quote_char, "#{escape_char}#{quote_char}")
  else
    e = self
  end
  "#{quote_char}#{e}#{quote_char}"
end

#unquote(quote_char = '"', escape_char = '\\') ⇒ Object Also known as: dequote



12
13
14
15
16
17
18
19
20
# File 'lib/str_quote/quote.rb', line 12

def unquote(quote_char = '"', escape_char = '\\')
  return self if self.length < 2 or self[0] != quote_char or self[-1] != quote_char
  
  unless escape_char.nil?
    self[1..-2].gsub("#{escape_char}#{quote_char}", quote_char)
  else
    self[1..-2]
  end
end