def name_repair(var)
car_names1 = ["HONDA",
"OLDSMOBILE",
"BUICK",
"HYUNDAI",
"CHEVROLET",
"ACURA",
"TOYOTA",
"CHRYSLER",
"DODGE",
"FORD",
"MITSUBISHI",
"PLYMOUTH",
"VOLKSWAGEN",
"PONTIAC",
"MAZDA",
"NISSAN",
"MINI",
"SATURN",
"GMC",
"INFINITI",
"MERCURY",
"BMW",
"CADILLAC",
"SUZUKI",
"JEEP",
"EAGLE",
"LEXUS",
"AUDI",
"KIA",
"VOLVO",
"SUBARU",
"JAGUAR",
"LINCOLN",
"ISUZU",
"GEO",
"MERCEDES BENZ"]
car_names1.each {|x|
if var[x]
return [var[x], var.gsub(x,'').strip]
end
}
return ["",""]
end
Refactorings
No refactoring yet !
Pavel Gorbokon
July 4, 2010, July 04, 2010 07:46, permalink
it will return [nil, nil] if it doesn't match to regexp if you need ["", ""] return [$1.to_s, $.to_s]
CAR_BRANDS = [
"HONDA",
"OLDSMOBILE",
"BUICK",
"HYUNDAI",
"CHEVROLET",
"ACURA",
"TOYOTA",
"CHRYSLER",
"DODGE",
"FORD",
"MITSUBISHI",
"PLYMOUTH",
"VOLKSWAGEN",
"PONTIAC",
"MAZDA",
"NISSAN",
"MINI",
"SATURN",
"GMC",
"INFINITI",
"MERCURY",
"BMW",
"CADILLAC",
"SUZUKI",
"JEEP",
"EAGLE",
"LEXUS",
"AUDI",
"KIA",
"VOLVO",
"SUBARU",
"JAGUAR",
"LINCOLN",
"ISUZU",
"GEO",
"MERCEDES BENZ"
].freeze
CAR_BRAND_REGEXP = /#{CAR_BRANDS.join('|')}/i
CAR_MODEL_REGEXP = /(.+?)/
CAR_NAME_PARSER_REGEXP = /^(#{CAR_BRAND_REGEXP})\s+(#{CAR_MODEL_REGEXP})$/i
def name_repair(name)
name.strip =~ CAR_NAME_PARSER_REGEXP
[$1, $2]
end
Adam
July 5, 2010, July 05, 2010 05:52, permalink
Just having some fun.
require 'rubygems'
require 'inline'
require 'set'
class Car
BRANDS = Set.new(%w(
HONDA OLDSMOBILE BUICK HYUNDAI CHEVROLET ACURA TOYOTA CHRYSLER DODGE
FORD MITSUBISHI PLYMOUTH VOLKSWAGEN PONTIAC MAZDA NISSAN MINI SATURN
GMC INFINITI MERCURY BMW CADILLAC SUZUKI JEEP EAGLE LEXUS AUDI KIA
VOLVO SUBARU JAGUAR LINCOLN ISUZU GEO MERCEDES\ BENZ
))
inline :C do |builder|
builder.c_raw %{
static VALUE name_repair(int argc, VALUE *argv, VALUE self) {
VALUE result = rb_ary_new2(2);
if (argc > 0) {
VALUE brands = rb_const_get(rb_funcall(self, rb_intern("class"), 0), rb_intern("BRANDS"));
ID include_sym = rb_intern("include?");
unsigned long length = RSTRING(argv[0])->len;
unsigned char *buffer = RSTRING(argv[0])->ptr;
unsigned long i;
for (i = length; i > 0; i--) {
if (buffer[i] == ' ') {
VALUE make = rb_str_new(buffer, i);
if (rb_funcall(brands, include_sym, 1, make) == Qtrue) {
rb_ary_push(result, make);
rb_ary_push(result, rb_str_new2(buffer + i + 1));
return result;
}
}
}
}
VALUE blank = rb_str_new2("");
rb_ary_push(result, blank);
rb_ary_push(result, blank);
return result;
}
}
end
end
What this method does is; I pass in a string like "MERCEDES BENZ C280W". The method then takes that string and compares it to the array of approved car names. If it exists it return the value in an array. The returned array would look like [MERCEDES BENZ, C280W] or [make, model]. I had alot of trouble with this, if I didnt have return ["",""] at the very end of the method then then method would end up returning the entire car_names1 array.
Also I have a if statement to check for returned results else where in the program;
if make != ""
dbh.do("INSERT INTO Car...