Dynamically changing class methods

H

henrikbrink

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:

load "#{file}.rb"
myclass = Object.const_get "MyClass"
myclass_inst = myclass.new
methods = myclass_inst.methods

If I am not clear enough, please ask.

Regards, Henrik
 
B

Bill Atkins

How are you removing the method?

Hello!
=20
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.
=20
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?
=20
Here's the code used to get the methods:
=20
load "#{file}.rb"
myclass =3D Object.const_get "MyClass"
myclass_inst =3D myclass.new
methods =3D myclass_inst.methods
=20
If I am not clear enough, please ask.
=20
Regards, Henrik
=20
=20
=20


--=20
Bill Atkins
 
D

David A. Black

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:

load "#{file}.rb"
myclass = Object.const_get "MyClass"
myclass_inst = myclass.new
methods = myclass_inst.methods

If you do this:

class MyClass
def some_method
# ...
end
end

and then, later, do this:

class MyClass
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
 
A

Ara.T.Howard

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:

load "#{file}.rb"
myclass = Object.const_get "MyClass"
myclass_inst = myclass.new
methods = myclass_inst.methods

If I am not clear enough, please ask.

Regards, Henrik

because:

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

irb(main):002:0> p Foo::instance_methods - Object::instance_methods
["foo"]

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

irb(main):004:0> p Foo::instance_methods - Object::instance_methods
["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:


jib:~ > cat b.rb

require 'dynaload'
loaded = Dynaload::dynaload 'a.rb'
loaded.classes.each do |klass, attributes|
p [klass, attributes]
p klass.instance_methods - Object.instance_methods
end


jib:~ > cat a.rb

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


jib:~ > ruby b.rb

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


jib:~ > cat a.rb

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


jib:~ > ruby b.rb

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


http://raa.ruby-lang.org/project/dynaload/
http://www.codeforpeople.com/lib/ruby/dynaload/

hth.

-a
--
===============================================================================
| 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
===============================================================================
 
H

henrikbrink

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top