Method: String#quote

Defined in:
lib/lite/ruby/string.rb

#quote(type = :double, amount = nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/lite/ruby/string.rb', line 367

def quote(type = :double, amount = nil)
  if type.is_a?(Integer)
    tmp = amount
    amount = type
    type = tmp || :mixed
  else
    amount ||= 1
  end

  case type.to_s
  when "'", 'single', 's', '1'
    f = "'" * amount
    b = f
  when '"', 'double', 'd', '2'
    f = '"' * amount
    b = f
  when '`', 'back', 'backtick', 'b', '-1'
    f = '`' * amount
    b = f
  when "`'", 'bracket', 'sb'
    f = '`' * amount
    b = "'" * amount
  when "'\"", 'mixed', 'm', 'Integer'
    c = (amount.to_f / 2).to_i
    f = '"' * c
    b = f

    if amount.odd?
      f = "'" + f
      b += "'"
    end
  else
    raise ArgumentError, "Invalid quote type: #{type.inspect}"
  end

  "#{f}#{self}#{b}"
end