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



32
33
34
35
36
# File 'lib/extensions/string.rb', line 32

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.



27
28
29
# File 'lib/extensions/string.rb', line 27

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