Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/absolute_renamer/libs/string.rb

Overview

Extension of existing String class

Instance Method Summary collapse

Instance Method Details

#camelize(word_separators = /[\W_]/) ⇒ Object

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"


9
10
11
# File 'lib/absolute_renamer/libs/string.rb', line 9

def camelize(word_separators = /[\W_]/)
    self.clone.camelize!(word_separators)
end

#camelize!(word_separators = /[\W_]/) ⇒ Object

Camelizes a string and returns it.

str = "Hello.THE World"
str.camelize!                  # => "Hello.The World"
str.camelize!(/[\.]/)          # => "Hello.The World"
str                            # => "Hello.The World"


18
19
20
21
22
23
24
25
26
# File 'lib/absolute_renamer/libs/string.rb', line 18

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