Sha256: 3e7f1a1e2333e52145ae63d8856999bdd45d3f78c708c6b72f58a24e83b4ca74

Contents?: true

Size: 787 Bytes

Versions: 1

Compression:

Stored size: 787 Bytes

Contents

require "string_plus/version"

module StringPlus
  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
    res = ""
    [nil, *self.each_char].each_cons(2) do |last, current|
      res << "_" if current == current.upcase && last != nil
      res << current.downcase
    end
    res
  end
  alias :snakecase :underscore
  
  def constantize
    c = self.split("-").map(&:camelcase).join("::")
    Object.send(:const_get, c)
  end
end

String.send(:include, StringPlus)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
string_plus-0.4.0 lib/string_plus.rb