# Dynamically changing class methods

**URL:** <https://rubytalk.org/t/dynamically-changing-class-methods/20095>\
**Category:** ruby-talk\
**Created:** [11 August 2005 15:46 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095 "2005-08-11T15:46:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![henrikbrink](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@henrikbrink](https://rubytalk.org/u/henrikbrink)\
**Post date:** [11 August 2005 15:46 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/1 "2005-08-11T15:46:11Z")

</div>

Hello!

Im making an application that loads and uses the methods of a class  
dynamically. The class can then be altered while the application is  
running, and the application loads the file containing the class every  
time it is needed.

The strange thing is: It works fine when i add a method to the class.  
The new method shows up. But if i remove a method, it acts as if it was  
still there. How can that be?

Here's the code used to get the methods:

&nbsp;&nbsp;&nbsp;&nbsp;load "#{file}.rb"  
&nbsp;&nbsp;&nbsp;&nbsp;myclass = Object.const\_get "MyClass"  
&nbsp;&nbsp;&nbsp;&nbsp;myclass\_inst = myclass.new  
&nbsp;&nbsp;&nbsp;&nbsp;methods = myclass\_inst.methods

If I am not clear enough, please ask.

Regards, Henrik

---

<div class="post-metadata">

**Author:** ![Bill\_Atkins](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bill\_Atkins](https://rubytalk.org/u/Bill_Atkins)\
**Post date:** [11 August 2005 15:56 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/2 "2005-08-11T15:56:15Z")

</div>

How are you removing the method?

> **···**
>
> On 8/11/05, henrikbrink \<henrikbrink@gmail.com\> wrote:
> 
> > Hello!
> > 
> > Im making an application that loads and uses the methods of a class  
> > dynamically. The class can then be altered while the application is  
> > running, and the application loads the file containing the class every  
> > time it is needed.
> > 
> > The strange thing is: It works fine when i add a method to the class.  
> > The new method shows up. But if i remove a method, it acts as if it was  
> > still there. How can that be?
> > 
> > Here's the code used to get the methods:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;load "#{file}.rb"  
> > &nbsp;&nbsp;&nbsp;&nbsp;myclass = Object.const\_get "MyClass"  
> > &nbsp;&nbsp;&nbsp;&nbsp;myclass\_inst = myclass.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;methods = myclass\_inst.methods
> > 
> > If I am not clear enough, please ask.
> > 
> > Regards, Henrik
> 
> --  
> Bill Atkins

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [11 August 2005 15:57 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/3 "2005-08-11T15:57:12Z")

</div>

Hi --

> Hello!
> 
> Im making an application that loads and uses the methods of a class  
> dynamically. The class can then be altered while the application is  
> running, and the application loads the file containing the class every  
> time it is needed.
> 
> The strange thing is: It works fine when i add a method to the class.  
> The new method shows up. But if i remove a method, it acts as if it was  
> still there. How can that be?
> 
> Here's the code used to get the methods:
> 
> &nbsp;&nbsp;&nbsp;load "#{file}.rb"  
> &nbsp;&nbsp;&nbsp;myclass = Object.const\_get "MyClass"  
> &nbsp;&nbsp;&nbsp;myclass\_inst = myclass.new  
> &nbsp;&nbsp;&nbsp;methods = myclass\_inst.methods

If you do this:

&nbsp;&nbsp;&nbsp;class MyClass  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def some\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# ...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

and then, later, do this:

&nbsp;&nbsp;&nbsp;class MyClass  
&nbsp;&nbsp;&nbsp;end

(i.e., don't define some\_method), some\_method is still there. The  
effects of opening a class multiple times are cumulative. This is the  
case whether it's all in one file or in multiple files, or even in the  
same file loaded multiple times.

If you want to remove a method, you have to use remove\_method or  
under\_method (which are slightly different from each other; see ri or  
other docs).

> If I am not clear enough, please ask.

Same here 🙂

David

> **···**
>
> On Fri, 12 Aug 2005, henrikbrink wrote:
> 
> --  
> David A. Black  
> dblack@wobblini.net

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [11 August 2005 16:00 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/4 "2005-08-11T16:00:56Z")

</div>

because:

&nbsp;&nbsp;&nbsp;harp:~ \> irb  
&nbsp;&nbsp;&nbsp;irb(main):001:0\> class Foo;def foo;end;end

&nbsp;&nbsp;&nbsp;irb(main):002:0\> p Foo::instance\_methods - Object::instance\_methods  
&nbsp;&nbsp;&nbsp;["foo"]

&nbsp;&nbsp;&nbsp;irb(main):003:0\> class Foo;def bar;end;end

&nbsp;&nbsp;&nbsp;irb(main):004:0\> p Foo::instance\_methods - Object::instance\_methods  
&nbsp;&nbsp;&nbsp;["bar", "foo"]

opening up a class, even in a file that's loaded, can only add or modify  
methods using 'def'. there is a mechanism to delete methods but it must be  
explicit.

for dynamically loading classes try this:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jib:~ \> cat b.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'dynaload'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loaded = Dynaload::dynaload 'a.rb'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loaded.classes.each do |klass, attributes|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p [klass, attributes]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p klass.instance\_methods - Object.instance\_methods  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jib:~ \> cat a.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'dynaload'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dynaload::export Foo, 'some attribute' =\> 42

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jib:~ \> ruby b.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[#\<Module:0xb75cded8\>::Foo, {"some attribute"=\>42}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["foo"]

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jib:~ \> cat a.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'dynaload'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class Foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dynaload::export Foo, 'some attribute' =\> 42

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jib:~ \> ruby b.rb

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[#\<Module:0xb75cbed8\>::Foo, {"some attribute"=\>42}]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;["bar"]

[http://raa.ruby-lang.org/project/dynaload/](http://raa.ruby-lang.org/project/dynaload/)

> **[Index of /lib/ruby/dynaload](https://www.codeforpeople.com/lib/ruby/dynaload/)**

hth.

-a

> **···**
>
> On Fri, 12 Aug 2005, henrikbrink wrote:
> 
> > Hello!
> > 
> > Im making an application that loads and uses the methods of a class  
> > dynamically. The class can then be altered while the application is  
> > running, and the application loads the file containing the class every  
> > time it is needed.
> > 
> > The strange thing is: It works fine when i add a method to the class.  
> > The new method shows up. But if i remove a method, it acts as if it was  
> > still there. How can that be?
> > 
> > Here's the code used to get the methods:
> > 
> > &nbsp;&nbsp;&nbsp;load "#{file}.rb"  
> > &nbsp;&nbsp;&nbsp;myclass = Object.const\_get "MyClass"  
> > &nbsp;&nbsp;&nbsp;myclass\_inst = myclass.new  
> > &nbsp;&nbsp;&nbsp;methods = myclass\_inst.methods
> > 
> > If I am not clear enough, please ask.
> > 
> > Regards, Henrik
> 
> # --
> 
> > email :: ara [dot] t [dot] howard [at] noaa [dot] gov  
> > phone :: 303.497.6469  
> > Your life dwells amoung the causes of death Like a lamp standing in a strong breeze. --Nagarjuna
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 August 2005 20:02 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/5 "2005-08-11T20:02:29Z")

</div>

Aside from the suggestions so far, you might want to take a look at  
[http://raa.ruby-lang.org/project/script/](http://raa.ruby-lang.org/project/script/). Just another approach to  
encapsulating the definitions in a loaded script file...

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![henrikbrink](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@henrikbrink](https://rubytalk.org/u/henrikbrink)\
**Post date:** [11 August 2005 19:56 UTC](https://rubytalk.org/t/dynamically-changing-class-methods/20095/6 "2005-08-11T19:56:10Z")

</div>

I can see now why my code didn't work, and i must say... doh!

But I'm glad I asked anyway as Dynaload does the job!

Thank you everyone!
