Ruby 如何获取和修改源代码?

linjunhalida · December 29, 2011 · 2209 hits

lisp 里面可以动态修改源代码,在 ruby 里面虽然可以重载和无数 hook, 但是是否有办法能够修改代码?其实只是需要能够获取代码就可以了, 是否能够实现下面的功能呢?

def helloworld n "hello #{n}" end

o = Kernel.method :helloword

Kernel.define_singleton_method o.name do |n| eval o.source_code.sub('hello', 'hello world') end

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.