Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:56:53+00:00 2026-06-12T06:56:53+00:00

In Ruby, I have a DAO class, which is extended by a class that

  • 0

In Ruby, I have a DAO class, which is extended by a class that makes managing the connections easier, which is extended by a class that represents and manipulates data in a DB, which is further extended by another class. To use an animal metaphor it would look like this:

class Animal
 ...
end

class Mammal < Animal
 ...
end

class Feline < Mammal
 ...
end

class Cat < Feline
 ...
end

class Lion < Cat
 ...
end

...

In PHP, there is __destruct method that runs when you destroy/delete a class. And should that class extend another class, you simply add parent::__destruct() to the class’s __destruct method like this:

public function __destruct() {
  // Clean up code for this class here
  ...

  // Execute clean up code for Parent class
  parent::__destruct();
}

I could have a similar method for all the classes except Animal. Since it doesn’t extend anything, the parent::__destruct(); line is no longer valid.

However, as I understand it, Ruby doesn’t have a method like this for its objects. A finalizer can be set, but I decided to just put in a cleanup method I can call whenever I want to destroy/delete a class. That would take care of anything that needed doing prior to my setting the class to nil.

This raises a new problem though. If the method is always named cleanup and I call lion_instance.cleanup, I assume it calls the Lion#cleanup. How then to get it to call the cleanup in class Cat and then Feline and on down the chain?

Or is this a wrong approach and you have a better idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:56:55+00:00Added an answer on June 12, 2026 at 6:56 am

    The Ruby idiom for this is to yield to a block which does work, and when the block returns, do cleanup. Ruby’s built-in “File.open” does this:

    File.open("/tmp/foo") do |file|
      file.puts "foo"
    end
    

    When the block ends, the file is closed for you, without you having to do anything. This is an excellent idiom. Here’s how you might implement something like that:

    class Foo
    
      def self.open(*args)
         foo = new(*args)
         yield foo
         foo.close
      end
    
      def initialize
        # do setup here
      end
    
      def close
        # do teardown here
      end
    
    end
    

    And to use it:

    Foo.open do |foo|
      # use foo
    end
    

    Foo#close will be caused automatically after the end


    This will work with subclassing as well. That’s because class methods are inherited just as are instance methods. Here’s the superclass:

    class Superclass
    
      def self.open(*args)
        o = new(*args)
        yield o
        o.close
      end
    
      def initialize
        # common setup behavior
      end
    
      def close
        # common cleanup behavior
      end
    
    end
    

    and two derived classes:

    class Foo < Superclass
    
      def initialize
        super
        # do subclass specific setup here
      end
    
      def close
        super
        # do subclass specific teardown here
      end
    
    end
    
    class Bar < Superclass
    
      def initialize
        super
        # do subclass specific setup here
      end
    
      def close
        super
        # do subclass specific teardown here
      end
    
    end
    

    to use:

    Foo.open do |foo|
      # use foo
    end
    
    Bar.open do |bar|
      # use bar
    end
    

    If you really need to make sure that cleanup happens no matter what, then use an ensure clause in the class method:

      def self.open(*args)
         foo = new(*args)
         begin
           yield foo
         ensure
           foo.close
         end
      end
    

    This way, cleanup happens even if there is an exception in the block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Ruby have a class that represents some period of time? IE, 1 hour,
I have Ruby script that creates a proxy so that I can make HTTP
so i have ruby script that simply writes to a test.txt file with hello
I have ruby code like this: module Hello class Hi def initialize() puts self.module.name
I have rails website that shows information from database. And I have ruby script
Does Ruby have any Formatter classes or methods that can be used to format
I have Ruby on Rails application which is using MySQL database. For now I'm
Background Perl and Ruby have the __END__ and __DATA__ tokens that allow embedding of
I have ruby build file that we used to read the assembly attributes that
My understanding was that ruby blocks have block scope, and all variables created inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.