class String
adds camelize and underscore to String
Taken from ruby on rails See apidock.com/rails/String/underscore See apidock.com/rails/String/camelize
Public Instance Methods
camelize(uppercase_first: false)
click to toggle source
# File lib/cardmarket_cli/util/string.rb, line 9 def camelize(uppercase_first: false) string = self string = if uppercase_first string.sub(/^[a-z\d]*/, &:capitalize) else string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase) end string.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }.gsub('/', '::') end
underscore()
click to toggle source
# File lib/cardmarket_cli/util/string.rb, line 19 def underscore return dup unless /[A-Z-]|::/.match?(self) word = gsub('::', '/').dup word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)((?=a)b)(?=\b|[^a-z])/) do "#{Regexp.last_match(1) && '_'}#{Regexp.last_match(2).downcase}" end word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr!('-', '_') word.downcase! word end