Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string_module.rb

Instance Method Summary collapse

Instance Method Details

#camelcase(*separators) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/string_module.rb', line 11

def camelcase(*separators)
  case separators.first
  when Symbol, TrueClass, FalseClass, NilClass
    first_letter = separators.shift
  end

  separators = ['_', '\s'] if separators.empty?

  str = self.dup

  separators.each do |s|
    str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase }
  end

  case first_letter
  when :upper, true
    str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase }
  when :lower, false
    str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase }
  end

  str
end

#clean_xmlObject



43
44
45
# File 'lib/string_module.rb', line 43

def clean_xml
  self.gsub(/<body\/>/, "")
end

#lower_camelcase(*separators) ⇒ Object



39
40
41
# File 'lib/string_module.rb', line 39

def lower_camelcase(*separators)
  camelcase(:lower, *separators)
end

#snakecaseObject



2
3
4
5
6
7
8
9
# File 'lib/string_module.rb', line 2

def snakecase
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr('-', '_').
  gsub(/\s/, '_').
  gsub(/__+/, '_').
  downcase
end

#upper_camelcase(*separators) ⇒ Object



35
36
37
# File 'lib/string_module.rb', line 35

def upper_camelcase(*separators)
  camelcase(:upper, *separators)
end