class String

ActiveSupport’s String methods become bottlenecks once:

With these optimizations, in sample scripts, garbage collection and gem requiring take up two-thirds of the running time.

Public Instance Methods

underscore() click to toggle source

Alternatively, check if ‘inflections.acronym_regex` is equal to `/(?=a)b/`. If so, to skip the substitution, which is guaranteed to fail.

@see api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-underscore

# File lib/pupa.rb, line 57
def underscore
  word = gsub('::', '/')
  # word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end