Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ext/string.rb
Overview
basic string extensions
these are pretty much ripped from:
github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
Instance Method Summary collapse
Instance Method Details
#camelcase ⇒ Object
9 10 11 12 13 14 |
# File 'lib/ext/string.rb', line 9 def camelcase string = self.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase } string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } string.gsub!('/', '::') string end |
#underscore ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ext/string.rb', line 16 def underscore acronym_regex = /(?=a)b/ word = self.gsub('::', '/') word.gsub!(/(?:([A-Za-z\d])|^)(#{acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" } 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 |