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

Your refactoring





Format Copy from initial code

or Cancel