Sha256: 11a43d69791bb64acfb87eb5688995c2f040dae536c8905472d3bd3a492068fd

Contents?: true

Size: 1002 Bytes

Versions: 6

Compression:

Stored size: 1002 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 & 2 rubygems

Version Path
simonc-AbsoluteRenamer-0.9.2 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.9.2 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.9.1 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.9.0 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.9.0.1 lib/absolute_renamer/libs/string.rb
AbsoluteRenamer-0.9.0.2 lib/absolute_renamer/libs/string.rb