Sha256: 82fb3b5b0e3827f5db433da1396eb5ab98ce8089736a1391f987be6d57f477ba

Contents?: true

Size: 916 Bytes

Versions: 1

Compression:

Stored size: 916 Bytes

Contents

require "string_plus/version"

module StringPlus
  module_function
  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

  def lcamelcase
    camelcase(false)
  end

  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
  alias :snakecase :underscore
  
  def constantize
    c = self.split("-").map(&:camelcase).join("::")
    Object.send(:const_get, c)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
string_plus-0.5.1 lib/string_plus.rb