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.



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

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'


22
23
24
25
26
# File 'lib/noble_names/core_ext/string.rb', line 22

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