Sha256: ea51626168b171096f976762269904ad9cd98c56e2abd3727f70eacaa2db0434

Contents?: true

Size: 978 Bytes

Versions: 6

Compression:

Stored size: 978 Bytes

Contents

# Extension of existing String class
class String
    # Returns a camelized version of a string
    # word_separator: a regular expression used to separate words.
    #    str = "hello.THE world"
    #    str.camelize           # => "Hello.The World"
    #    str.camelize(/[\.]/)   # => "Hello.The world"
    #    str                    # => "hello.THE world"
    def camelize(word_separators = /[\W_]/)
        self.clone.camelize!(word_separators)
    end

    # Camelizes a string and returns it.
    #    str = "Hello.THE World"
    #    str.camelize!          # => "Hello.The World"
    #    str.camelize!(/[\.]/)  # => "Hello.The World"
    #    str                    # => "Hello.The World"
    def camelize!(word_separators = /[\W_]/)
        self.downcase!
        self.each_char.each_with_index do |c,i|
            if self[i-1].chr =~ word_separators or i.zero?
                self[i] = c.upcase if c =~ /[a-z]/
            end
        end
        self
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
AbsoluteRenamer-1.0.3 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-1.0.2 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-1.0.1 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-1.0.0 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.10.0 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.10.1 lib/absolute_renamer/libs/string.rb