Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/cardmarket_cli/util/string.rb
Overview
adds camelize and underscore to String Taken from ruby on rails See https://apidock.com/rails/String/underscore See https://apidock.com/rails/String/camelize
Instance Method Summary collapse
Instance Method Details
#camelize(uppercase_first: false) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# 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 ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 |