Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/extensions/string.rb
Instance Method Summary collapse
- #camelize(first_letter = :upper) ⇒ Object
- #constantize ⇒ Object
- #to_numbers(replace = '#') ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#camelize(first_letter = :upper) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/extensions/string.rb', line 7 def camelize(first_letter = :upper) case first_letter when :upper to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } when :lower first.downcase + camelize(self)[1..-1] end end |
#constantize ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/extensions/string.rb', line 25 def constantize names = self.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#to_numbers(replace = '#') ⇒ Object
2 3 4 |
# File 'lib/extensions/string.rb', line 2 def to_numbers(replace='#') self.gsub(/#{replace}/){ Kernel.rand(10) } end |
#underscore ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/extensions/string.rb', line 16 def underscore to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |