From: Christer Nilsson Date: 2006-09-09T17:41:55+09:00 Subject: How to read and write 80 column punched cards with Ruby I'm trying to communicate with a bank. Banks are still using the punched card layout in their files. Example: 001234567800023420060909 (account=12345678, amount=234 and day=20060909) I'm trying to make some nice Ruby classes to handle this. I would like to define the class like this (compare attr) class Trans < Record field :account, 10 field :amount, 6 field :lastday, 8 end This is what I've been able to do: (There are several problems: As the fields have to be in a certain order a hash will not work. On the other hand, looping through an array takes time. Another is the problem of setting an attribute to nil, trans.account=nil) class Field attr_accessor :name, :size, :pad, :value def initialize(name,size,pad) @name=name @size=size @pad=pad end end class Record def initialize @fields = [] end def field name, size, pad @fields << Field.new(name.to_s, size, pad) end def save s p=0 @fields.each do |f| f.value = s[p,f.size] p += f.size end end def to_s s="" @fields.each do |f| s += f.value.to_s end s end def method_missing(id, value=nil) if value==nil then @fields.each do |f| return f.value if f.name == id.to_s end else x = id.to_s.gsub("=", "") @fields.each do |f| f.value = value if f.name == x end end end end class Trans < Record def initialize super field :account, 10 field :amount, 6 field :lastday, 8 end end trans = Trans.new trans.save "001234567800023420060909" puts trans.account trans.amount = 120 puts trans /Christer -- Posted via http://www.ruby-forum.com/.