Class: String

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

Instance Method Summary collapse

Instance Method Details

#camel_caseObject

Convert to camel case

"foo_bar".camel_case          #=> "fooBar"


5
6
7
8
# File 'lib/judopay/core_ext/string.rb', line 5

def camel_case
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map(&:capitalize).join.uncapitalize
end

#uncapitalizeObject

Convert first letter to lower case

"BananaMan".uncapitalize      #=> "bananaMan"


13
14
15
# File 'lib/judopay/core_ext/string.rb', line 13

def uncapitalize
  self[0, 1].downcase + self[1..-1]
end

#underscoreObject

Converts a camel_cased string to a underscored string Replaces spaces with underscores, and strips whitespace

"BananaMan".underscore        #=> "banana_man"


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/judopay/core_ext/string.rb', line 21

def underscore
  to_s
    .strip
    .tr(' ', '_')
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .squeeze('_')
    .downcase
end