Garbage collection

I think this is a simple question but I just down't know where to find
the answer. Any help would be appreciated.

I have the following code:

class MMR
  attr_accessor :id, :name, :cap

  @@header = []

  def initHeader(fld)
    @@header = fld
  end

  def initInstance(fld, cap=0)
    @@header.each_with_index {|fn, i| send("#{fn}=", fld[i])}
    self
  end
end

MMR.new.initHeader(['id', 'name', 'cap'])
# The above line instanciates an MMR object and initializes @@header.
# My question is, since I don't have a variable pointing to this newly
# instantiated MMR object, can it be removed by the garbage collector?
# Is this dangerous?

# When I run the program it works just fine but I'm afraid it may not
@lst << MMR.new.initHeader(['1', 'disk1', '1073741824'])
@lst << MMR.new.initHeader(['2', 'disk2', '10737418240'])

Thanks
Hector

···

--
Posted via http://www.ruby-forum.com/.

It is assigning a class var, so you do have an active reference to it: the MMR class. Unless you make the class get collected (hard to do), it is safe for the span of your program's run.

···

On Jan 26, 2012, at 8:04, Hector Quiroz <hectorquiroz1@gmail.com> wrote:

MMR.new.initHeader(['id', 'name', 'cap'])
# The above line instanciates an MMR object and initializes @@header.
# My question is, since I don't have a variable pointing to this newly
# instantiated MMR object, can it be removed by the garbage collector?
# Is this dangerous?