From: Wyss Clemens Date: 2001-03-03T01:11:46+09:00 Subject: [ruby-talk:11902] Class Destructor? How about using define_finalizer in your initialize method. Read: http://www.ruby.ch/ProgrammingRuby/htmlC/ref_m_kernel.html#exit or http://www.ruby.ch/ProgrammingRuby/htmlC/ref_m_objectspace.html#define_final izer Clemens > -----Urspr�ngliche Nachricht----- > Von: Jim Freeze [mailto:jim@freeze.org] > Gesendet am: Freitag, 2. M�rz 2001 16:54 > An: ruby-talk@ruby-lang.org > Betreff: [ruby-talk:11901] Class Destructor? > > I have a class where the initializer takes a filename > and opens a file. The class has several methods to let > the user get information from the file. > > The problem is, I don't know how to have the class > close the file automatically like a destructor would > in C++. (Maybe I have missed something big here) > > I see 2 options: > > 1) Put the burden on the user with #begin #end > > begin > c = Myclass.new("file") > c.meth1 > c.meth2 > > ensure > c.close > end > > > 2) Or I could store the file pointer in the class and > have each method open the file, move to the proper location, > do its thing, save the file pointer, close the file. > But this seems kind of kludgy. > > > Any other ideas out there? > > Thanks > > > > ========================================================= > Jim Freeze > jim@freeze.org > --------------------------------------------------------- > "So, it should be relatively easy ... this is the phrase > I use when someone else is the most obvious person to do > something." > Harry Ohlsen > ========================================================= >