Note that there are some explanatory texts on larger screens.

plurals
  1. POFinalizers in Ruby: is there an equivalent to "__destruct" from PHP?
    primarykey
    data
    text
    <p>Within PHP classes the parser deals with the <code>__construct</code> and <code>__destruct</code> methods to instantiate the instance and destroy it when the script exits or you use unset. When you extend a class you simply use <code>parent::__construct</code> and <code>parent::__destruct</code> to run any cleanup code that might need running on the class that was extended.</p> <p>Now within the context of a class that represents DB data and helps you manipulate that data I'd thought that a <code>__destruct</code> method could be used to compare current values against the originals grabbed from the DB and do an UPDATE when necessary (in some cases just always do an UPDATE so long as the Primary Key value cannot be changed). Implementing this in PHP is pretty straight forward.</p> <p>The main upside to this approach would be to simply manipulate class variables quickly as needed and then have the class do one big UPDATE at the end. In long scripts that run for minutes it might be nice to create DB instance during __construct, get the data, close the DB connection, and then manipulate class variables only during the minutes long execution. On __destruct, open up a new DB connection make the UPDATEs and then close down the DB connection and clean up anything else that needs cleaning up.</p> <p>I'm curious what people's thoughts are on whether or not this is a good idea/bad practice but my main question was is this possible in Ruby.</p> <p>In Ruby you have the initialize method that runs when you instantiate an instance of the class. The Ruby equivalent of <code>parent::__construct</code> is <code>super</code> in Ruby. And there is the <code>ObjectSpace.define_finalize</code> and <code>finalize</code> method for Ruby classes. However, as I understand it, the finalize method isn't supposed to be able to reference the instance calling it. On top of that I can't find any equivalent to <code>parent::__destruct</code>. I suspect that's because there isn't an equivalent since it seems that the <code>finalize</code> method was explicitly designed to prevent referencing itself.</p> <p>Anyone out there know of way to do this? If not, what's the best practice for dumping Ruby classes to get back resources and prevent data loss? Does everyone have a garbage_collection method they call just before setting a class instance to nil or is there some other way?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload