------ art_37604_23509059.1216604085589
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline
Mine:
require 'ostruct'
class AryOfRecs
def initialize
@ary = []
end
def <<(record)
@ary << record
keys = ( record.respond_to?("marshal_dump") ? record.marshal_dump.keys :
record.instance_variables)
keys.each { |key|
roa_way(key.to_s.sub("@","")) if
record.respond_to?(key.to_s.sub("@",""))
}
end
def [](index)
return @ary[index]
end
def to_a
@ary
end
private
def roa_way(attrib)
instance_eval(
%(def #{attrib}
@ary.map { |rec| rec.#{attrib} }
end))
end
end
I only made the wrapper for an array of records, I't can be filled as in the
example:
data = AryOfRecs.new
File.open("data.txt").each do |line|
name, age, color = line.chomp.split(/,/)
d = OpenStruct.new
d.name = name
d.age = age
d.color = color
data << d
end
puts data[2].name
puts data.name[2]
Or with a class:
class Person
attr_accessor :name, :age, :color
end
data = AryOfRecs.new
File.open("data.txt").each do |line|
name, age, color = line.chomp.split(/,/)
d = Person.new
d.name = name
d.age = age
d.color = color
data << d
end
puts data[2].name
puts data.name[1]
--
Ash Mac durbatulū°, ash Mac gimbatul, ash Mac thrakatulū° agh burzum-ishi
krimpatul.
Juanger. http://xocoruby.blogspot.com
------ art_37604_23509059.1216604085589--