Project

destructor

0.0
No commit activity in last 3 years
No release in over 3 years
Add finalize method callback for Ruby instances on class definition
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0
 Project Readme

Destructor

This Add new callback to instance objects such as :initialize, but for the opposite effect: finalize

Installation

Add this line to your application's Gemfile:

gem 'destructor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install destructor

Usage

    require 'destructor'
    
    class Foo
    
      attr_reader :bar
    
      def initialize
        @bar = 123
      end
    
      def finalize
        puts "Before Destroying OBJECT: #{self.inspect}"
      end
    
    end
    
    
    f=Foo.new
    
    f.instance_variable_set(:@bar,rand)
    puts "Foo.bar is #{f.bar} now"
    
    f=nil # mark for sweep for GC
    
    # Force ruby to start the Garbage Collector
    # In a real program you don't have to do this
    # ruby will run the GC automatically.
    GC.start
    
    sleep 3 # make sure you will see the message
    # before ruby quits
    
    puts 'done'
    
    #> Before Destroying OBJECT: #<Foo:0x007fdc298c66c8 @bar=0.8856108662303454>

Contributing

  1. Fork it ( https://github.com/[my-github-username]/destructor/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request