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 502433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:17:27+00:00 2026-05-13T06:17:27+00:00

I did a little experiments with Ruby class dynamic loading/unloading/updating as implementing plugins infrastructure.

  • 0

I did a little experiments with Ruby class dynamic loading/unloading/updating as implementing plugins infrastructure. I found a few points:

  1. If loading new version of same class without first unloading it, the new one essentially ‘top’ or ‘merge’ with previous version. All existent objects created with previous version would get their class definition ‘updated’.
  2. Unloading a class does not affect existent objects created with this class. Existent objects stay with whatever version just unloaded. (class cannot be used anymore but not the objects already created)
  3. If loading new version after unloading of previous version, new objects created would be of the new version. However, old objects created before the loading of new version would not be affected and would still be of the old version.

My question is, is there an easy way to make existent object created from the old class version ‘switch’ to the new version (but not a merged version of the old & new version)? It seems to me the possible way to do it is to re-create the object after unloading/loading, which is not suitable for plugins (don’t want it be destroyed).

Update: my intent was to have existent objects updated with new version, without the problem of merging old version with new version (like change of number of arguments, or the removal of a method). Unloading and then reloading again seems to be the cleanest way of doing this, though you must keep track of all such objects and recreate them when needed. Also, expensive objects might not be suitable for re-creation. This leaves me with the second option, prohibiting unexpected merging from happening. As long as no method removed, no method signature changed, the merging should work just fine.

Below is my test program:

$ cat test.rb
load 'v1.rb'
puts "=> 'v1.rb' loaded"
a1 = A.new
puts "=> object a1(#{a1}) created"
a1.common
a1.method_v1
load 'v2.rb'
puts '',"=> class A updated by 'v2.rb'"
a1.common
a1.method_v1
a1.method_v2

a2 = A.new
puts '',"=> object a2(#{a2}) created"
a2.common
a2.method_v1
a2.method_v2

Object.send(:remove_const, 'A')
puts '',"=> class A unloaded"

A.new rescue puts $!

puts '',"=> class A does not exist now"
a1.common
a1.method_v1
a1.method_v2 rescue puts $!
a2.common
a2.method_v1
a2.method_v2

load 'v3.rb'
puts '',"=> 'v3.rb' loaded"
a1.common
a1.method_v1
a1.method_v2 rescue puts $!
a1.method_v3 rescue puts $!
a2.common
a2.method_v1
a2.method_v2
a2.method_v3 rescue puts $!

a3 = A.new
puts '',"=> object a3(#{a3}) create"
a3.common
a3.method_v1 rescue puts $!
a3.method_v2 rescue puts $!
a3.method_v3

The sample output:

$ ruby test.rb
=> 'v1.rb' loaded
=> object a1(#<A:0x1042d4b0>) created
#<A:0x1042d4b0>: common: v1
#<A:0x1042d4b0>: method v1

=> class A updated by 'v2.rb'
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2

=> object a2(#<A:0x1042cec0>) created
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2

=> class A unloaded
uninitialized constant A

=> class A does not exist now
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2

=> 'v3.rb' loaded
#<A:0x1042d4b0>: common: v2
#<A:0x1042d4b0>: method v1
#<A:0x1042d4b0>: method v2
undefined method `method_v3' for #<A:0x1042d4b0>
#<A:0x1042cec0>: common: v2
#<A:0x1042cec0>: method v1
#<A:0x1042cec0>: method v2
undefined method `method_v3' for #<A:0x1042cec0>

=> object a3(#<A:0x1042c3f8>) create
#<A:0x1042c3f8>: common: v3
undefined method `method_v1' for #<A:0x1042c3f8>
undefined method `method_v2' for #<A:0x1042c3f8>
#<A:0x1042c3f8>: method v3

Below is the 3 versions of class A:

$ cat v1.rb
class A
  def common
    puts "#{self}: common: v1"
  end
  def method_v1
    puts "#{self}: method v1"
  end
end

$ cat v2.rb
class A
  def common
    puts "#{self}: common: v2"
  end
  def method_v2
    puts "#{self}: method v2"
  end
end

$ cat v3.rb
class A
  def common
    puts "#{self}: common: v3"
  end
  def method_v3
    puts "#{self}: method v3"
  end
end
  • 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-05-13T06:17:27+00:00Added an answer on May 13, 2026 at 6:17 am

    Obviously there’s a danger in totally replacing the class definition with a new class definition, whether you’re merging the new version or deleting the old version and expecting objects to automatically get updated. That danger is in the fact that the old version of the object may be in an invalid state for the new version. (For example, instance variables that the new version of the class initializies in its initialize method may not have been defined by the old version, but there could aso be subtler bugs than this). So care (and a well-planned upgrade path) is needed no matter how you pull this off.

    Given that you know what the version you’re upgrading from looks like (which you need in order to upgrade sensibly anyway), it’s dead simple to have the new version of the class remove unneeded methods from the old version of the class:

    class A
      remove_method :foo
    end
    

    And I’m not sure what you’re talking about when you say there’s problems redefining a method to take a different number of parameters. It works fine for me:

    class A
      def foo a
        a
      end
    end
    ainst=A.new
    p(ainst.foo 1) rescue puts($!)
    p(ainst.foo 1,2) rescue puts($!)
    
    class A
      def foo a,b
        [a,b]
      end
    end
    p(ainst.foo 1) rescue puts($!)
    p(ainst.foo 1,2) rescue puts($!)
    

    The only thing you can’t do (AFAIK) is change the class’s superclass. That’s defined the first time you define the class, and you’re not allowed to change it (though you can specify the same ancestor class again).

    class A < Object
    end
    class A < Object
    end
    class A < String #TypeError: superclass mismatch for class A
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 265k
  • Answers 265k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The documentation is correct that getLoaderInfoByDefinition is available only to… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer The simple answer is to setup your associations in reverse… May 13, 2026 at 12:22 pm
  • Editorial Team
    Editorial Team added an answer This little segment of code might help. var $dl =… May 13, 2026 at 12:22 pm

Related Questions

I used SPRY about a year ago to experiment with the fact that you
I am very experienced in working with open-source technologies like PHP, MySQL, Apache and
Did I say array enough for you there? To clarify: I'm working on organizing
I've been investigating the effort needed in getting menu items displayed in bold face
I have a web app that is being hit by facebook. The login page

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.