ruby code for modifying outer quotes on strings?

前端 未结 3 735
孤城傲影
孤城傲影 2021-01-19 06:28

Does anyone know of a Ruby gem (or built-in, or native syntax, for that matter) that operates on the outer quote marks of strings?

I find myself writing methods like

3条回答
  •  孤街浪徒
    2021-01-19 07:14

    I would use the value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0]). In short, this simple code checks whether first and last char of string are the same and removes them if they are single/double quote. Additionally as many as needed quote types can be added.

    %w["adadasd" 'asdasdasd' 'asdasdasd"].each do |value|
      puts 'Original value: ' + value
      value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0])
      puts 'Processed value: ' + value
    end
    

    The example above will print the following:

    Original value: "adadasd"
    Processed value: adadasd
    Original value: 'asdasdasd'
    Processed value: asdasdasd
    Original value: 'asdasdasd"
    Processed value: 'asdasdasd"
    

提交回复
热议问题