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

  • SEARCH
  • Home
  • 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 8906227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:35:55+00:00 2026-06-15T02:35:55+00:00

Does anything special happen to objects when there are no variables pointing to them

  • 0

Does anything special happen to objects when there are no variables pointing to them any more? Apart from being eligible for garbage collection, that is.

For example, currently, IO objects automatically close when they’re garbage collected, if they haven’t done so already. Even if you wanted to change this behavior, there isn’t any way of changing it so that automatic closing occurs when no more variables point at the object, is there?

(My question is a slight simplification: WeakRef allows variables to point to objects and for them to be targets of garbage collection. Also, it’s possible to access objects that don’t have any variables pointing to them, for some implementations of Ruby, by using ObjectSpace. And regarding IO objects, there’s things like IO.open(&block) that automatically close IO objects after the block has been executed.)

  • 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-15T02:35:57+00:00Added an answer on June 15, 2026 at 2:35 am

    No, there’s no hook or special method that gets executed when there are no more references to an object.

    The specific cases you mention are:

    • automatic closing of resources on garbage collection: this is achieved with a finalizer

    • automatic closing of resources at the end of a block: no magic here

      class IO
        def self.open(*args)
          yield file_handle = new(*args)
        ensure
          file_handle.close
        end
      end
      
    • WeakRef: there is magic here 🙂 In YARV, lib/weakref.rb uses ::ObjectSpace::WeakMap, which provides the weak reference semantics. JRuby implements WeakMap using Java’s native weak reference semantics. IOW: this cannot actually be expressed in Ruby, it has to be provided by the runtime system.

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

Sidebar

Related Questions

The TextMate2 (Alpha 9090) terminal support does not install anything in any directory. Is
Does .NET natively support anything similar to PHP's variable variables ? If not, how
Question: Where does p get it's value from below and why does it happen?
it doesn't seem that jquery ui themes does anything to multiselect listboxes. does anyone
Does .NET have anything similar to Perl arrays, which are indexed numerically but automatically
Unfortunately the specification does not tell anything about precision. The xml scheme definition states
PHP Echo or Print functions does not show anything when php is busy with
I'm reading PEP 8 and notice that it does not say anything on maximum
Please could someone explain to me why this regex does not match anything, when
For whatever reason, std::cout does not display anything with my application. The description of

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.