2010/4/7 Brk Anl said:
Actually i try to fill a table
class Sorm_Table
def initialize =A0# define table structure and array for data
@template =3D
Struct.new
nofcall,
bj,:rpn,:rac,:cg1,:cgpn,:cd1,:cdpn,:stt)
There is no point in recreating this class over and over again. Rather do
class Sorm_Table
Template =3D Struct.new
nofcall,
bj,:rpn,:rac,:cg1,:cgpn,:cd1,:cdpn,:st=
t)
@data=3DArray.new(1000){Array.new()}
There is an inconsistency in the way you fill @data: you initialize it
with a certain size and stuff Array instances in there and later you
stuff a single @data (or Template with my modification) instances in
every nested Array. At the moment your nested Arrays look completely
superfluous.
Note also that pre allocation of an Array is not necessary in Ruby -
Arrays can grow.
No idea what that is used for. As far as I can see there is no usage
of this variable. The fact though that you put strings in there that
look like integers is suspicious. If these are to be treated as ints
then make them ints.
end # initialize
def add_row(_nofcall,_obj, _rpn,_rac,_cg1,_cgpn,_cd1,_cdpn,_stt)
[email protected](_nofcall,_obj, _rpn,_rac,_cg1,_cgpn,_cd1,_cdpn,_stt)
@data[$indx] << s
$indx=3D$indx+1
end
Modifying a global variable from inside a class instance is a very bad
idea. Remember that you define a class in order to have _multiple_
instances of it. So your $indx should rather be @indx but more likely
it's completely superfluous. You can simply append to the array.
Your #add_row could then look like this:
def add_row(*args)
@data << Template.new(*args)
self
end
after i call add row func
tbl.add_row($callno,tbl.obj[$index2],final_data[Enum:
N2],final_data[Enu= m::AC2],final_data[Enum:
N1],final_data[Enum::AC1],final_data[Enum:
N2],f=
inal_data[Enum::AC2],'nACT')
i try to add these datas like @data[0][0]=3D$callno
@data[0][1]=3Dtbl.obj[$index2]
A global variable name with a number in there looks suspicious as
well. It is most likely that you want to store the index in
Sorm_Table instances or even just use @data.size.
so i can reach the $callno of the fist row of the table easily but now
@data[0] carries all of the data i can not seperate. Cause of this i am
trying to split.
How can i do it?
Frankly, the logic of your application is not fully clear to me. It
seems though that some more changes are in order. Can you provide a
description of what you want to do? You should at least provide an
explanation what $callno and all the other variables mean and what
types you stuff in there.
Kind regards
robert
--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/