Skip to content

A few Ruby methods demonstrate string/array manipulation to separate names in the format of 'Last, First Middle' or even 'Last, First Middle1 Middle2'.

Notifications You must be signed in to change notification settings

ZASMan/ruby_name_parser_methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 

Repository files navigation

Basic Ruby Name Parser (Simple String/Array Manipulation Example)

Note: I don't claim that this is a super efficient way to do things, instead I want to share this as a simple example of array/string manipulation in the Ruby Programming language for beginners. For a more comprehensive tool, I suggest a gem such as Namae.

This had a specific use case in which I had a CSV spreadsheet of several hundredvalues where every name was a value in the format of Last, First Middle or even Last, First Middle1 Middle2. To match up with my data model structure, I needed to extract a first, middle, and last name. After using Ruby's CSV class to pull the raw name data, I used the following simple methods to extract the names.

Note: the user_full_name variable comes from the value pulled from the CSV file.

# Example user_full_name_variable
user_full_name = 'Anderson Wayne, Bruce Tom'

def set_first_name(user_full_name)
  unless user_full_name.nil?
    # This compensates for a format such as
    # Anderson Wayne, Bruce Tom
    # and assumes that Bruce is
    # the first name
    full_name_split_comma = user_full_name.split(',', 0)
    first_and_middle_name = full_name_split_comma[1]
    first_and_middle_name = first_and_middle_name.split(" ")
    first_name = first_and_middle_name[0]
  end
end

def set_last_name(user_full_name)
  unless user_full_name.nil?
    # Split by ',' because , will always come after last name
    user_full_name.strip.split(",")[0]
  end
end

def set_middle_name(user_full_name)
  unless user_full_name.nil?
    names_array = user_full_name.strip.split(",")
    # If there's no middle name, stop here
    # It will be equal to 1 if just 'Wayne, Bruce'
    if names_array[1].split(" ").length == 1
      ""
    else
      # This compensates for case of a name such as
      # Anderson Wayne, Bruce Tom James
      # in the case that "Tom" and "James" are both
      # the middle name to keep both as "Tom James"
      first_and_middle = names_array[1]
      first_and_middle_array = first_and_middle.split(" ")
      first_and_middle_array.delete(first_and_middle_array[0])
      first_and_middle_array.join(" ")
    end
  end
end

About

A few Ruby methods demonstrate string/array manipulation to separate names in the format of 'Last, First Middle' or even 'Last, First Middle1 Middle2'.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published