Sha256: 7564dd42a600dc2210526d5a4fb2172c73513a17c1b6df759e8d399a84a29280

Contents?: true

Size: 534 Bytes

Versions: 1

Compression:

Stored size: 534 Bytes

Contents

class String

  def underscore()
    camel_cased_word = self
    acronym_regex = /#{["REST"].join("|")}/
    return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
    word = camel_cased_word.to_s.gsub('::'.freeze, '/'.freeze)
    word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" }
    word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
    word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
    word.tr!("-".freeze, "_".freeze)
    word.downcase!
    word
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toolhound-ruby-1.0.4 lib/toolhound-ruby/core_ext/string.rb