There is no intended end use for this. It is for learning and development only.
I got carried away following an example and ended up with a miniaturised vehicle registration system. You can bulk generate records, each of which is given a unique registration number in keeping with British registration format. I've written a script that compares and lists all registration numbers that fall afoul of the criteria.
It does work, but it is incredibly slow. (over an hour to check 1 mil records). I am looking for critique on the logic and any optimisation I may have missed.
Example string: AA99AAA
Example criteria: A?9?AAA
def full_search(offensive_list)
p 'Full check:'
p "Comparing #{$all_vehicles.count} records against #{offensive_list.count} banned combinations"
p 'This will take a few minutes'
vrm_array, example_array = [], []
vrm_list = $all_vehicles.keys.sort
vrm_list.each do |vrm|
vrm_array << vrm.split("") #We split each reg into an array of characters
end
offensive_list.each do |example|
example.strip!
example_array << example.split("") #and the same with our banned combinations
end
vrm_array.each do |vrm|
example_array.each do |example| #itterate through vrms x examples
@formatted_vrm = vrm.dup
if example.length == vrm.length
example.each_index do |index|
if example[index] == "?" #for each wildcard we add a wildcard to the vrm for comparison
@formatted_vrm[index] = "?"
end
end
if @formatted_vrm == example then offensive_found(vrm, example) end
end
end
end
end
def offensive_found(vrm, example)
built_vrm = ""
built_example = ""
if vrm.class == Array #clean up formatting so we can store it
vrm.each do |character|
built_vrm << character
end
example.each do |character|
built_example << character
end
else
built_example = example #clearly redundant, but it works so...
built_vrm = vrm
end
if $bad_vrms[built_example] # if we already have a record
prev_matched = $bad_vrms[built_example] #just add to the array
prev_matched << built_vrm
$bad_vrms.store(built_example, prev_matched)
else
new_match = [built_vrm] # or create a new hash key
$bad_vrms.store(built_example, new_match)
end
#p "#{built_vrm} - matched with #{built_example}"
end
If you'd prefer you can clone the full thing on github. https://github.com/Niall47/RubySomDemo