Module: NobleNames::CoreExt::String

Defined in:
lib/noble_names/core_ext/string.rb

Overview

This Module gives String the capability to titleize itself.

Instance Method Summary collapse

Instance Method Details

#to_titleString

Capitalizes each Word in a name except for those which are nobility particles

Examples:

Titleize names

'jamie jones'.to_title      # => 'Jamie Jones'
'jamie of windsor'.to_title # => 'Jamie of Windsor'

Returns:

  • (String)

    name a new String matching the old self titleized.



14
15
16
# File 'lib/noble_names/core_ext/string.rb', line 14

def to_title
  dup.to_title!
end

#to_title!Object

Does the same as #to_title but replaces the old string.

Examples:

Titleize a name

str = 'jamie of windsor'
str.to_title!
str                     #=> 'Jamie of Windsor'


23
24
25
26
27
28
29
30
31
# File 'lib/noble_names/core_ext/string.rb', line 23

def to_title!
  words = split(/\s+/)
  if words.any? { |w| NobleNames.business_particle? w }
    NobleNames.correct_business_particles words
  else
    words.map! { |w| NobleNames.noble_capitalize(w) }
  end
  replace(words * ' ')
end