Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/ruby/string/camelize.rb,
lib/core_ext/ruby/string/underscore.rb

Instance Method Summary collapse

Instance Method Details

#camelizeObject



2
3
4
# File 'lib/core_ext/ruby/string/camelize.rb', line 2

def camelize
  to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#underscoreObject



2
3
4
5
6
7
8
9
10
11
# File 'lib/core_ext/ruby/string/underscore.rb', line 2

def underscore
  # self[0, 1].downcase + self[1..-1].gsub(/[A-Z]/) { |c| "_#{c.downcase}" }
  word = to_s
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end