jmcnevin / rubypants

RubyPants: SmartyPants for Ruby
Other
29 stars 20 forks source link

Reduce calls to :gsub with a Hash #28

Closed ashmaroli closed 3 years ago

ashmaroli commented 4 years ago

String#gsub also accepts a Hash to replace matches in a single go. Code such as the following can be optimized with a constant:

  def process_escapes(str)
    str.
      gsub('\\\\', '\').
      gsub('\"',   '"').
      gsub("\\\'", ''').
      gsub('\.',   '.').
      gsub('\-',   '-').
      gsub('\`',   '`')
  end