Class: String

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

Overview

Custom extensions to the class String

Instance Method Summary collapse

Instance Method Details

#camel_caseObject

Change a ruby_cased string to CamelCased



30
31
32
33
34
# File 'lib/extensions/string.rb', line 30

def camel_case
  self.split(/_/).map { |i|
    i.sub(/^./) { |s| s.upcase }
  }.join
end

#ruby_caseObject

Change CamelCased strings to ruby_cased strings It uses the lookahead assertion ?= In this case it basically says match anything followed by a capital letter, but not the capital letter itself.



25
26
27
# File 'lib/extensions/string.rb', line 25

def ruby_case
  self.split(/(?=[A-Z])/).join('_').downcase
end