Skip to content

Commit

Permalink
- Refactored String#tokenize
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jan 16, 2009
1 parent a5a1ffd commit 72b7d47
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/commander/core_ext/string.rb
Expand Up @@ -2,17 +2,15 @@
class String

##
# Replace _hash_ keys with associated values. Mutative.
# Replace +hash+ keys with associated values. Mutative.

def tokenize! hash
hash.each_pair do |k, v|
self.gsub! Regexp.new(":#{k}"), v.to_s
end
hash.each_pair { |k, v| gsub! /:#{k}/, v }
self
end

##
# Replace _hash_ keys with associated values.
# Replace +hash+ keys with associated values.

def tokenize hash
self.dup.tokenize! hash
Expand All @@ -23,11 +21,11 @@ def tokenize hash

def camelcase upcase_first_letter = true
up = upcase_first_letter
str = dup
str.gsub!(/\/(.?)/){ "::#{$1.upcase}" }
str.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
str.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase } if up
str
s = dup
s.gsub!(/\/(.?)/){ "::#{$1.upcase}" }
s.gsub!(/(?:_+|-+)([a-z])/){ $1.upcase }
s.gsub!(/(\A|\s)([a-z])/){ $1 + $2.upcase } if up
s
end

end

0 comments on commit 72b7d47

Please sign in to comment.