Module: StrQuote::Quote
- Included in:
- String
- Defined in:
- lib/str_quote/quote.rb
Instance Method Summary collapse
- #quote(opts = {}) ⇒ Object
- #unquote(opts = {}) ⇒ Object (also: #dequote)
Instance Method Details
#quote(opts = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/str_quote/quote.rb', line 3 def quote(opts = {}) opts = {quote_char: '"', escape_char: '\\'}.merge opts unless opts[:escape_char].nil? e = self.gsub(opts[:quote_char], "#{opts[:escape_char]}#{opts[:quote_char]}") else e = self end "#{opts[:quote_char]}#{e}#{opts[:quote_char]}" end |
#unquote(opts = {}) ⇒ Object Also known as: dequote
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/str_quote/quote.rb', line 13 def unquote(opts = {}) opts = {quote_char: ['"',"'"], strip: true, escape_char: '\\'}.merge opts # Should we strip this string first? if opts[:strip] s = self.strip else s = self end # Get the quote string into an array, to simplify the logic below if opts[:quote_char].is_a? String opts[:quote_char] = [ opts[:quote_char] ] end # If it's too short to quote, we're done. return s if s.length < 2 # Now it has to be quoted before we do anything. opts[:quote_char].each do |qc| if s[0..qc.length-1] == qc and s[0-qc.length..-1] == qc unless opts[:escape_char].nil? return s[qc.length..0-(qc.length+1)].gsub("#{opts[:escape_char]}#{qc}", qc) else return s[qc.length..0-(qc.length+1)] end end end # Not quoted according to the rules passed to us s end |