Self modifying code is a well known technology. Has anyone done this in
Ruby? What I’m seeking for is examples of software that adds to
itself (methods and variables) in runtime.
I understand from reading the Ruby documentation that it is easy
to add a method programmatically to a running object instance, but
is it also possible to maintain changes (persistance) after system
halts?
“Sten Kvamme” skvamme@as4-2-8.an.g.bonet.se schrieb im Newsbeitrag
news:3DF19A9C-7B0D-11D7-93CD-003065DC4D52@as4-2-8.an.g.bonet.se…
Hello,
Self modifying code is a well known technology. Has anyone done this in
Ruby? What I’m seeking for is examples of software that adds to
itself (methods and variables) in runtime.
I understand from reading the Ruby documentation that it is easy
to add a method programmatically to a running object instance, but
is it also possible to maintain changes (persistance) after system
halts?
I think not directly. I guess you couls store a map from method name to
method code and recreate the methods after loading.
class Foo
def initialize() @dynamic_methods =
end
some code that adds to @dynamic_methods
def after_load() @dynamic_methods.each do |code|
# code is something like “def foo(); puts ‘foo’; end”
instance_eval code
end
end
end
In fact, you can redefine your entire program while it is live. Once I wrote a text editor using Tk that had this type of dynamic behavior. My first goal was to be able to write the program from within the program and it was pretty easy to accomplish using a text widget and a function that would call an eval function in the main/base class.
The hard part, as you point out, is capturing the changes. There are some possibilities that occurred to me. I was working on my program’s actual source code the whole time, so I just saved the file with the changes before using the eval function. This brings up the issue of version control-- you need to keep a safe working copy, while keeping your test copy.
I think as I redevelop that idea using gtk2 I will take the approach of saving the code incrementally. The code is all just text getting sent to eval in a separate eval function. So what I’m thinking now is that each snippet of code would get saved to a file. The files would go in a “delta” directory and follow a sequential naming pattern.
That way you’ve got all the code that was used to create your program’s current state (data aside). Then the main application code just needs a final function right before your main loop or function call that looks for any “delta” fragments and requires them in the right order. You could also (either by hand or programmatically) merge the deltas back into the original code when it made sense to do so.
I understand from reading the Ruby documentation that it is easy
to add a method programmatically to a running object instance, but
is it also possible to maintain changes (persistance) after system
halts?