module StringPlus

Constants

VERSION

Public Instance Methods

camelcase(str=self, capitalize_first_char=true) click to toggle source
# File lib/string_plus.rb, line 5
def camelcase(str=self, capitalize_first_char=true)
  res = ""
  flag = capitalize_first_char
  str.each_char {|w|
    (flag = true ; next) if w == "_" || w == " "
    res << if flag
             flag = false
             w.upcase
           else
             w
           end
  }
  res
end
constantize(str=self) click to toggle source
# File lib/string_plus.rb, line 35
def constantize(str=self)
  c = str.split("-").map {|segment| camelcase(segment)}.join("::")
  Object.send(:const_get, c)
end
lcamelcase(str=self) click to toggle source
# File lib/string_plus.rb, line 20
def lcamelcase(str=self)
  camelcase(str, false)
end
snakecase(str=self)
Alias for: underscore
underscore(str=self) click to toggle source
# File lib/string_plus.rb, line 24
def underscore(str=self)
  return str.downcase if str.each_char.all? { |c| c == c.upcase && c =~ /[a-zA-Z0-9]/}
  res = ""
  [nil, *str.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
Also aliased as: snakecase