Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ovhrb/core_additions.rb
Instance Method Summary collapse
- #camelize(first_letter = :lower) ⇒ Object
- #constantize ⇒ Object
- #humanize ⇒ Object
- #titleize ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#camelize(first_letter = :lower) ⇒ Object
2 3 4 5 6 7 8 |
# File 'lib/ovhrb/core_additions.rb', line 2 def camelize(first_letter = :lower) if first_letter != :lower gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self[0].chr.downcase + camelize(self)[1..-1] end end |
#constantize ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/ovhrb/core_additions.rb', line 10 def constantize unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end Object.module_eval("::#{$1}", __FILE__, __LINE__) end |
#humanize ⇒ Object
22 23 24 |
# File 'lib/ovhrb/core_additions.rb', line 22 def humanize gsub(/_/, " ").capitalize end |
#titleize ⇒ Object
26 27 28 |
# File 'lib/ovhrb/core_additions.rb', line 26 def titleize underscore.humanize.gsub(/\b('?[a-z])/) { $1.capitalize } end |
#underscore ⇒ Object
18 19 20 |
# File 'lib/ovhrb/core_additions.rb', line 18 def underscore gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase end |