Skip to content

Instantly share code, notes, and snippets.

@anthony-ryan
Created October 15, 2013 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anthony-ryan/6990934 to your computer and use it in GitHub Desktop.
Save anthony-ryan/6990934 to your computer and use it in GitHub Desktop.
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
@cavebatsofware
Copy link

Line 3: The file name and the record delimiter should be passed in separately.

@cavebatsofware
Copy link

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