Skip to content

Instantly share code, notes, and snippets.

@coty
Forked from sarchertech/multi_store_finder.rb
Created April 20, 2011 21:52
Show Gist options
  • Save coty/932961 to your computer and use it in GitHub Desktop.
Save coty/932961 to your computer and use it in GitHub Desktop.
This requires Ruby 1.9. It will run under 1.8 because uniq! will ignore the block, but it won't be correct.
require 'yaml'
class Store
attr_accessor :name, :address, :city, :state, :zip, :phone_number
def unique_attributes
[name[0..5], zip, phone_number, address[0..3]]
end
end
def list_of_stores
files = Dir.glob('*.yml')
stores = []
files.each do |file|
File.open(file, 'r') {|f| stores += YAML.load(f)}
end
return stores
end
def delete_duplicates(stores)
original_length = stores.length
stores.uniq! {|s| s.unique_attributes}
return original_length - stores.length
end
def print_multi_stores(stores)
seen = {}
stores.each do |store|
if seen.has_key?(store.name)
seen[store.name][0] += 1
else
seen[store.name] = [1, store.state]
end
end
seen = seen.sort_by {|k,v| v[0]}
seen.reverse!
puts ""
puts "multi stores"
puts "-------------"
#sorting converts hash to array of arrays
seen.each do |k,v|
num, state = v
puts num.to_s + "--" + state + "--" + k if num > 1
end
puts "-------------"
puts ""
end
stores = list_of_stores
stores.sort_by! {|s| s.zip}
puts stores.length
puts "deleted " + delete_duplicates(stores).to_s + " duplicate stores"
print_multi_stores(stores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment