Skip to content

Instantly share code, notes, and snippets.

@jboursiquot
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jboursiquot/2f8d69e81a8e96d4bbce to your computer and use it in GitHub Desktop.
Save jboursiquot/2f8d69e81a8e96d4bbce to your computer and use it in GitHub Desktop.
Hashes and collections

Given a CSV document with three columns, firstname, lastname, and email, please read that file into a data structure that I can use to:

  1. List every person found in the CSV by firstname, lastname and email
  2. Find a person's information by email
@caramcc
Copy link

caramcc commented Mar 16, 2015

def read_csv_file(csv_file_path)
  csv_data = []
  CSV.foreach(csv_file_path) do |row|
    csv_data.push({
      firstname: row.field('firstname'),
      lastname: row.field('lastname'),
      email: row.field('email')
    })
  end
  csv_data
end

# List every person by firstname, lastname, email

def first_last_email(csv_data)
  csv_data.each do |data|
    puts "#{data[:firstname]}, #{data[:lastname]}, #{data[:email]}""
  end
end

# find person's information by email

def find_by_email(csv_data, email)
  csv_data.each do |data|
    if data.email == email
      return data
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment