Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/ruby_warrior/core_additions.rb
Instance Method Summary collapse
- #camelize ⇒ Object
- #constantize ⇒ Object
- #hard_wrap(width = 80) ⇒ Object
- #humanize ⇒ Object
- #titleize ⇒ Object
- #underscore ⇒ Object
Instance Method Details
#camelize ⇒ Object
2 3 4 |
# File 'lib/ruby_warrior/core_additions.rb', line 2 def camelize gsub(/(?:^|_)(.)/) { $1.upcase } end |
#constantize ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/ruby_warrior/core_additions.rb', line 6 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 |
#hard_wrap(width = 80) ⇒ Object
26 27 28 |
# File 'lib/ruby_warrior/core_additions.rb', line 26 def hard_wrap(width = 80) gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip end |
#humanize ⇒ Object
18 19 20 |
# File 'lib/ruby_warrior/core_additions.rb', line 18 def humanize gsub(/_/, " ").capitalize end |
#titleize ⇒ Object
22 23 24 |
# File 'lib/ruby_warrior/core_additions.rb', line 22 def titleize underscore.humanize.gsub(/\b('?[a-z])/) { $1.capitalize } end |
#underscore ⇒ Object
14 15 16 |
# File 'lib/ruby_warrior/core_additions.rb', line 14 def underscore gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase end |