Module: Corefines::String::Camelcase
- Defined in:
- lib/corefines/string.rb
Instance Method Summary collapse
-
#camelcase ⇒ String
Returns a copy of the str converted to camelcase.
Instance Method Details
#camelcase(*separators) ⇒ String #camelcase(first_letter, *separators) ⇒ String
Returns a copy of the str converted to camelcase. When no :upper or
:lower is specified, then it leaves the first character of a word
(after camelization) unchanged.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/corefines/string.rb', line 46 module Camelcase refine ::String do def camelcase(*separators) first_letter = separators.shift if ::Symbol === separators.first separators = [/_+/, /\s+/] if separators.empty? self.dup.tap do |str| separators.compact.each do |s| s = "(?:#{::Regexp.escape(s)})+" unless s.is_a? ::Regexp str.gsub!(/#{s}([a-z\d])/i) { $1.upcase } end case first_letter when :upper str.gsub!(/(\A|\s)([a-z])/) { $1 + $2.upcase } when :lower str.gsub!(/(\A|\s)([A-Z])/) { $1 + $2.downcase } end end end end end |