Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ruby_ext/core/string.rb
Instance Method Summary collapse
- #camelize(first_letter_in_uppercase = true) ⇒ Object
- #constantize ⇒ Object
- #dirname ⇒ Object
- #expand_path ⇒ Object
- #indent(spaces = 2) ⇒ Object
- #rsplit(*args) ⇒ Object
- #substitute(*args) ⇒ Object
- #substitute!(*args) ⇒ Object
- #to_a ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#camelize(first_letter_in_uppercase = true) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/ruby_ext/core/string.rb', line 33 def camelize first_letter_in_uppercase = true if first_letter_in_uppercase gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize ⇒ Object
41 42 43 44 |
# File 'lib/ruby_ext/core/string.rb', line 41 def constantize names = sub(/^::/, '').split '::' names.reduce(Object){|memo, name| memo.const_get name, false} end |
#dirname ⇒ Object
11 12 13 |
# File 'lib/ruby_ext/core/string.rb', line 11 def dirname File.(File.dirname(self)) end |
#expand_path ⇒ Object
15 16 17 |
# File 'lib/ruby_ext/core/string.rb', line 15 def File.(self) end |
#indent(spaces = 2) ⇒ Object
2 3 4 5 |
# File 'lib/ruby_ext/core/string.rb', line 2 def indent spaces = 2 indent = ' ' * spaces gsub /^/, indent end |
#rsplit(*args) ⇒ Object
7 8 9 |
# File 'lib/ruby_ext/core/string.rb', line 7 def rsplit *args reverse.split(*args).collect(&:reverse).reverse end |
#substitute(*args) ⇒ Object
46 47 48 |
# File 'lib/ruby_ext/core/string.rb', line 46 def substitute(*args) gsub(*args){yield Regexp.last_match.captures} end |
#substitute!(*args) ⇒ Object
50 51 52 |
# File 'lib/ruby_ext/core/string.rb', line 50 def substitute!(*args) gsub!(*args){yield Regexp.last_match.captures} end |
#to_a ⇒ Object
19 20 21 |
# File 'lib/ruby_ext/core/string.rb', line 19 def to_a [self] end |
#underscore ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/ruby_ext/core/string.rb', line 23 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 |