What's the correspond method of PERL's DESTROY in ruby

Y

Yi Zhang

------=_NextPart_000_0170_01C5734D.1A8866B0
Content-Type: text/plain;
charset="US-ASCII"
Content-Transfer-Encoding: 7bit

Hello



What's the corresponding method of PERL's DESTROY in ruby?



Thanks,

Yi Zhang


------=_NextPart_000_0170_01C5734D.1A8866B0--
 
T

Timothy Hunter

Yi said:
What's the corresponding method of PERL's DESTROY in ruby?

Generally speaking there is no equivalent. Like Perl, Ruby's garbage
collection cleans up unreferenced objects without any intervention. If
you want do something to clean up an external resource at the point when
you're finished using the instance, the cooresponding Ruby idiom is to
use a block. Check out http://www.rubygarden.org/ruby?RubyIdioms.
 
F

Florian Groß

Timothy said:
Generally speaking there is no equivalent. Like Perl, Ruby's garbage
collection cleans up unreferenced objects without any intervention. If
you want do something to clean up an external resource at the point when
you're finished using the instance, the cooresponding Ruby idiom is to
use a block. Check out http://www.rubygarden.org/ruby?RubyIdioms.

There is ObjectSpace.define_finalizer(obj) { ... }, but it should
generally be avoided as there is trouble when the block references the
object and that it is usually bad practice.

Using free callbacks in C extensions makes sense, but if you find
yourself using destructors in Ruby you are usually doing something odd.
 
G

Garance A Drosehn

=20
What's the corresponding method of PERL's DESTROY in ruby?
=20
I am not aware of any direct equivalent to DESTROY, but there are
some tricks you can use in perl which *might* make the automatic
garbage collection work better for you.

If you have a ruby object which includes some large arrays or
hashes in the object, then you might want to call the 'clear'
method on each of those arrays when you're done with the object.
And after sending the 'clear', you could set the variable-name
to nil. I have used this in a few scripts, and it has helped to
reduce the memory requirements of the script.

So, for some of the classes I create, I have a 'clear' method,
and that does something like:

def clear
@array1.clear
@array1 =3D nil
@hashtab.clear
@hashtab =3D nil
end

That doesn't really destroy the object, but it makes it easier for
ruby to do some of it's garbage-collection. In some situations this
has made a significant difference for me. In other situations, it
doesn't seem to make any noticeable difference.

--=20
Garance Alistair Drosehn =3D (e-mail address removed)
Senior Systems Programmer or (e-mail address removed)
Rensselaer Polytechnic Institute; Troy, NY; USA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top