Module: StringPlus

Defined in:
lib/string_plus.rb,
lib/string_plus/version.rb

Constant Summary collapse

VERSION =
"0.5.1"

Class Method Summary collapse

Class Method Details

.camelcase(capitalize_first_char = true) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/string_plus.rb', line 5

def camelcase(capitalize_first_char=true)
  res = ""
  flag = capitalize_first_char
  self.each_char {|w|
    (flag = true ; next) if w == "_" || w == " "
    res << if flag
             flag = false
             w.upcase
           else
             w
           end
  }
  res
end

.constantizeObject



35
36
37
38
# File 'lib/string_plus.rb', line 35

def constantize
  c = self.split("-").map(&:camelcase).join("::")
  Object.send(:const_get, c)
end

.lcamelcaseObject



20
21
22
# File 'lib/string_plus.rb', line 20

def lcamelcase
  camelcase(false)
end

.underscoreObject Also known as: snakecase



24
25
26
27
28
29
30
31
32
# File 'lib/string_plus.rb', line 24

def underscore
  return self.downcase if self.each_char.all? { |c| c == c.upcase && c =~ /[a-zA-Z0-9]/}
  res = ""
  [nil, *self.each_char].each_cons(2) do |last, current|
    res << "_" if last !=nil && current == current.upcase && !("0".."9").include?(current) 
    res << current.downcase unless current == " "
  end
  res
end