Module: StringPlus

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

Constant Summary collapse

VERSION =
"0.5.0"

Instance Method Summary collapse

Instance Method Details

#camelcase(capitalize_first_char = true) ⇒ Object



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

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



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

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

#lcamelcaseObject



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

def lcamelcase
  camelcase(false)
end

#underscoreObject Also known as: snakecase



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

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