ruby – Replace a long case statement involving regex with a hash
I would use a single regex and a hash code that does not use regex, just the output of a
single regex match to do the code lookup.
Demo : www.jdoodle.com/ia/1NBq
file_types = {
'camt.053.001' => 'camt053',
'camt.052.001' => 'camt052',
'"Auftragskonto";"Buchungstag";"Valutadatum";' => 'SpK'
}
RxFileTypes = /(?:camt\.053\.001|camt\.052\.001\.08|"Auftragskonto";"Buchungstag";"Valutadatum";)/
first_bytes="
Output
File type code is : camt053
Combine all the file types into a single regex separated by alternations.
The match will be the Key into the File Type code hash.
(?:
camt \. 053 \. 001
| camt \. 052 \. 001 \. 08
| "Auftragskonto";"Buchungstag";"Valutadatum";
)
For additional File types just add another alternation and an entry into the File Type code hash.
Read more here: Source link
