Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/core_additions.rb

Instance Method Summary collapse

Instance Method Details

#camelizeObject



2
3
4
# File 'lib/ruby_warrior/core_additions.rb', line 2

def camelize
  gsub(/(?:^|_)(.)/) { $1.upcase }
end

#constantizeObject



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

#humanizeObject



18
19
20
# File 'lib/ruby_warrior/core_additions.rb', line 18

def humanize
  gsub(/_/, " ").capitalize
end

#titleizeObject



22
23
24
# File 'lib/ruby_warrior/core_additions.rb', line 22

def titleize
  underscore.humanize.gsub(/\b('?[a-z])/) { $1.capitalize }
end

#underscoreObject



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