I'd like to get the bytecode of a method in Ruby 1.9. I know that I
can compile a string or file into bytecode with:
iseq = RubyVM::InstructionSequence.compile_file("name_of_file")
But is there a method which allows me to do access the bytecode
of a method from a running script as shown below:
class A
def b
1 + 2
end
end
iseq = A.bytecode_for_method(:b) # => RubyVM::InstructionSequence
If not, is it possible/hard to implement? I think there should be a
method cache somewhere which contains an instruction pointer to the
start of the method.
I'd love to experiment with translating the bytecode into a different
language (maybe Javascript), that's why I am ask :)
Regards,
Michael
FYI, a project that translates YARV bytecode into JavaScript already exists:
David Majda
--
Everyone gets everything he wants. --Captain Willard in Apocalypse Now
Personal :: da...@majda.cz :: www.majda.cz
Work :: david...@impaladesign.cz :: www.impaladesign.cz
Thanks. I already know of this project (which is great btw) and I am
working on something similar (http://www.ntecs.de/projects/rubyjs), but
I want to generate Javascript instead of interpreting Ruby in
Javascript. A combination of both would of course be perfect :)
Regards,
Michael
I solved this myself with extending class Method and UnboundMethod for
method #iseq. See the attached patch.
Regards,
Michael