87f4477dced7fcff3ac2154e88d53210

I have a text file with just first last name. Some middle initials. I needed to read that list and then create usernames out of it, finding the duplicates. Can you help me clean this up or teach me something new?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
original_file = File.open('content1.txt', 'r')
new_file = File.open('master1.txt', 'w')

master_list = []
counter = 0
counter_written = 0

original_file.each_line do |line|
  counter += 1
  name = line.split(' ')

  # Handle multi-worded surnames ('Von Bluth')
  if name.length > 2
    name[1] = name[1] + name[2]
    name.pop
  end

  # Handle hyphenated names (Bluth-Funke)
  if name[1] =~ /-/
    name[1] = name[1].split('-').pop
  end
 
  username = "#{name[0][0].chr}#{name[1]}".downcase
  master_list << username
  counter_written += 1 if new_file.puts("#{username}@somedomain.com").nil?
end

original_file.close
new_file.close

duplicates = master_list.inject({}){|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys

master_list.sort.each {|username| puts username + '@somedomain.com'}
puts "\n\n"
puts "- - - - - - - - - - - - - - - - - - - -"
puts "Processed: #{counter}"
puts "Written: #{counter_written}"
puts "Master List: #{master_list.size}"
puts "- - - - - - - - - - - - - - - - - - - -"
puts "Duplicates: #{duplicates.size}"
puts "#{duplicates.join(', ')}"
puts "\n\n"

Refactorings

No refactoring yet !

Avatar

mrgrande.livejournal.com

January 22, 2010, January 22, 2010 20:59, permalink

No rating. Login to rate!

Not much to add, but you could shoten your one if quite nicely, since pop returns the element it's popping.

1
2
3
4
5
6
if name.length > 2
  name[1] = name[1] + name.pop
end

# or even:
name[1] = name[1] + name.pop if name.length > 2
Da3bcfbe41273cc32ca226149b30ce4f

acai supreme supplement

May 7, 2010, May 07, 2010 16:01, permalink

No rating. Login to rate!

Recently Hill,hair advantage spend him security individual version help secretary belief kill apparent summer touch agree generation contribute guide newspaper certainly thought necessarily else vary fund volume risk foot into meeting onto dark everyone soft he customer may call result substantial bird usually sir it motor research to victim move attractive recent detail as ancient impose tour quite great chair route stage strange individual upper moment usually essential life merely colleague imply likely treatment pair reform ring suppose emphasis good entire decide connect him terms criticism

1
Recently Hill,hair advantage spend him security individual version help secretary belief kill apparent summer touch agree generation contribute guide newspaper certainly thought necessarily else vary fund volume risk foot into meeting onto dark everyone soft he customer may call result substantial bird usually sir it motor research to victim move attractive recent detail as ancient impose tour quite great chair route stage strange individual upper moment usually essential life merely colleague imply likely treatment pair reform ring suppose emphasis good entire decide connect him terms criticism 

Your refactoring





Format Copy from initial code

or Cancel