Class: String

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

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/core_ext/string.rb', line 2

def blank?
  empty? || /\A[[:space:]]*\z/.match?(self)
end

#to_camel_case(uppercase_first_letter = false) ⇒ Object

Convert string to CamelCase



7
8
9
10
11
12
13
14
15
# File 'lib/core_ext/string.rb', line 7

def to_camel_case(uppercase_first_letter = false)
  string = self
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
  else
    string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
  end
  string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
end

#to_snake_caseObject

Convert string to snake_case



18
19
20
21
22
23
24
# File 'lib/core_ext/string.rb', line 18

def to_snake_case
  self.gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
end