Module: Fletcher::String
- Defined in:
- lib/fletcher/string.rb
Overview
This class extends string functions
Instance Method Summary collapse
- #camelize(first_letter_in_uppercase = true) ⇒ Object
- #constantize ⇒ Object
- #sanitize ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#camelize(first_letter_in_uppercase = true) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/fletcher/string.rb', line 8 def camelize(first_letter_in_uppercase = true) lower_case_and_underscored_word = self.dup.underscore if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } else lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize ⇒ Object
17 18 19 |
# File 'lib/fletcher/string.rb', line 17 def constantize self.split("::").inject(Module) {|acc, val| acc.const_get(val)} end |
#sanitize ⇒ Object
4 5 6 |
# File 'lib/fletcher/string.rb', line 4 def sanitize self.strip end |
#underscore ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/fletcher/string.rb', line 21 def underscore word = self.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |