Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/frenchy/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#camelizeObject



11
12
13
# File 'lib/frenchy/core_ext.rb', line 11

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

#constantizeObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/frenchy/core_ext.rb', line 15

def constantize
  names = self.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end

  constant
end

#underscoreObject



27
28
29
30
31
32
33
# File 'lib/frenchy/core_ext.rb', line 27

def underscore
  dup.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end