Hi,
I want to be able to modify Ruby code at runtime. I know that it is
possible to add classes/methods at runtime. But that is not what I want.
I want to insert statements or method calls at runtime. For example if I
have a method:
def foo a
x = a + 5
end
I want to change it at runtime to:
def foo a
puts a
x = a + 5
end
That means a Ruby application has to be able to modify itself. I am
pretty sure that this is not trivial.
My approach would be as follows:
1. Learn how MRI works internally. I was already able to download and
compile the interpreter. But I don't know any good sources which
describes how the interpeter works internally. A book that I have came
across is "Ruby under a microscope"
(http://patshaughnessy.net/ruby-under-a-microscope). Is this book
recommendable?
2. Learn how YARV Bytecode works. Maybe I can add/modify the bytecode
instructions at runtime. But I don't know how complicated this is.
Another approach that came into my mind would be to intercept the
loader, inject my statements (like "puts a" above) and send the modified
file to the loader. The problem is that this can't be done at runtime,
since a ruby script gets loaded only once when the application starts.
What do you think?
···
--
Posted via http://www.ruby-forum.com/.