In my day job, I work a lot with barcodes. We develop Windows Mobile software for devices with barcode readers. From time to time, I need to do file manipulation chores. That is: colleagues give me text files that contain barcodes and ask me to transform these.
Yesterday, a colleague of mine asked me to take as input a file with 12 characters long barcodes and add a check digit assuming the barcodes are EAN13. I decided to do it and also start maintaining a set of barcode helper classes written in Ruby. I haven’t created a gem yet, cause the functionality is … lets say limited :-), but I plan to do so later.
You can go to
and just download the barcode.rb file.
Here is how I used the barcode.rb file in order to transform the file:
file = File.open("apog.txt")
file.each do |line|
bc = line.slice(0..11)
bc13 = Barcode.new(bc).get_ean_13
puts bc13 + line.slice(12..line.length)
The file had the following format:
and for the three lines above it produces:
Update: I have changed this project to a gem. You can read about it in my PAbarcode gem post.