Created
October 15, 2013 12:36
-
-
Save anthony-ryan/6990934 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileReader | |
def read_file(text, file_delim, field_delim) | |
result = [] | |
lines_array = text.split([file_delim].pack('H*')) | |
lines_array.each do |line| | |
result << fields = line.split([field_delim].pack('H*')) | |
fields.each do |field| | |
puts field | |
end | |
end | |
result | |
end | |
def get_file_delim(filename) | |
if filename.match('.edi') | |
file_delim = '0d0a' | |
elsif filename.match('.csv') | |
file_delim = '0a' | |
else | |
puts 'unknown file' | |
file_delim = nil | |
end | |
return file_delim | |
end | |
def get_field_delim(filename) | |
if filename.match('.edi') | |
field_delim = '07' | |
elsif filename.match('.csv') | |
field_delim = "2c" | |
else | |
puts 'unknown file' | |
field_delim = nil | |
end | |
return field_delim | |
end | |
end |
Line 15: Ruby convention would be record_delim returns the @record_delim value rather than the getter setter pattern. Really we should let the user of the class set the delimiters rather than trying to guess them. Setting reasonable defaults of \n and , would be a good idea though.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 3: The file name and the record delimiter should be passed in separately.