Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ext/string.rb
Instance Method Summary collapse
-
#camelize(uppercase_first_letter = true) ⇒ Object
Barrow these from ActiveSupport.
- #constantize ⇒ Object
-
#from_json ⇒ Object
Custom additions.
- #underscore ⇒ Object
Instance Method Details
#camelize(uppercase_first_letter = true) ⇒ Object
Barrow these from ActiveSupport
4 5 6 7 8 9 10 |
# File 'lib/ext/string.rb', line 4 def camelize(uppercase_first_letter = true) string = self.dup if uppercase_first_letter string = string.sub(/^[a-z\d]*/) { $&.capitalize } end string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }.gsub('/', '::') end |
#constantize ⇒ Object
23 24 25 |
# File 'lib/ext/string.rb', line 23 def constantize Object.const_get(self) end |
#from_json ⇒ Object
Custom additions
28 29 30 |
# File 'lib/ext/string.rb', line 28 def from_json JSON.parse(self) end |
#underscore ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ext/string.rb', line 12 def underscore word = self.dup word.gsub!('::', '/') word.gsub!(/(?:([A-Za-z\d])|^)((?=a)b)(?=\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 |