Class: ICU::Name

Inherits:
Object
  • Object
show all
Defined in:
lib/icu_tournament/name.rb

Overview

Names

This class exists for two main reasons:

  • to normalise to a common format the different ways names are typed in practice

  • to be able to match two names even if they are not exactly the same

To create a name object, supply both the first and second names separately to the constructor.

robert = ICU::Name.new(' robert  j ', ' FISHER ')

Capitalisation, white space and punctuation will all be automatically corrected:

robert.name                                    # => 'Robert J. Fischer'
robert.rname                                   # => 'Fischer, Robert J.'  (reversed name)

To avoid ambiguity when either the first or second names consist of multiple words, it is better to supply the two separately, if known. However, the full name can be supplied alone to the constructor and a guess will be made as to the first and last names.

bobby = ICU::Name.new(' bobby fischer ')

bobby.first                                    # => 'Bobby'
bobby.last                                     # => 'Fischer'

Names will match even if one is missing middle initials or if a nickname is used for one of the first names.

bobby.match(robert)                            # =>  true

Note that the class is aware of only common nicknames (e.g. Bobby and Robert, Bill and William, etc), not all possibilities.

Supplying the match method with strings is equivalent to instantiating a Name instance with the same strings and then matching it. So, for example the following are equivalent:

robert.match('R. J.', 'Fischer')               # => true
robert.match(ICU::Name('R. J.', 'Fischer'))    # => true

In those examples, the inital R matches the first letter of Robert. However, nickname matches will not always work with initials. In the next example, the initial R does not match the first letter B of the nickname Bobby.

bobby.match('R. J.', 'Fischer')                # => false

Some of the ways last names are canonicalised are illustrated below:

ICU::Name.new('John', 'O Reilly').last         # => "O'Reilly"
ICU::Name.new('dave', 'mcmanus').last          # => "McManus"
ICU::Name.new('pete', 'MACMANUS').last         # => "MacManus"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name1 = '', name2 = '') ⇒ Name

Returns a new instance of Name.



59
60
61
62
63
# File 'lib/icu_tournament/name.rb', line 59

def initialize(name1='', name2='')
  @name1 = name1.to_s
  @name2 = name2.to_s
  canonicalize
end

Instance Attribute Details

#firstObject (readonly)

Returns the value of attribute first.



57
58
59
# File 'lib/icu_tournament/name.rb', line 57

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



57
58
59
# File 'lib/icu_tournament/name.rb', line 57

def last
  @last
end

Instance Method Details

#match(name1 = '', name2 = '') ⇒ Object



85
86
87
88
# File 'lib/icu_tournament/name.rb', line 85

def match(name1='', name2='')
  other = Name.new(name1, name2)
  match_first(first, other.first) && match_last(last, other.last)
end

#nameObject



65
66
67
68
69
70
71
# File 'lib/icu_tournament/name.rb', line 65

def name
  name = ''
  name << @first
  name << ' ' if @first.length > 0 && @last.length > 0
  name << @last
  name
end

#rnameObject



73
74
75
76
77
78
79
# File 'lib/icu_tournament/name.rb', line 73

def rname
  name = ''
  name << @last
  name << ', ' if @first.length > 0 && @last.length > 0
  name << @first
  name
end

#to_sObject



81
82
83
# File 'lib/icu_tournament/name.rb', line 81

def to_s
  rname
end