Is it possible to see the ruby code in a proc?

后端 未结 5 1044
别那么骄傲
别那么骄傲 2021-02-05 15:52
p = Proc.new{ puts \'ok\' }

Is is possible to see the ruby code in the proc?

inspect returns the memory location:

         


        
5条回答
  •  旧时难觅i
    2021-02-05 16:39

    No, there is no way to do that in Ruby.

    Some Ruby implementations may or may not have implementation-specific ways of getting the source code.

    You can also try to use Proc#source_location to find the file that the Proc was defined in, and then parse that file to find the source code. But that won't work if the Proc wasn't defined in a file (e.g. if it was defined dynamically with eval) or if the source file no longer exists, e.g. because you are running an AOT-compiled version of your program.

    So, the short answer is: no, there is no way. The long answer is: there are some ways that may or may not sometimes work depending on way too many factors to even begin to make this work reliably.

    That's not even taking into account Procs which don't even have a Ruby source code because they were defined in native code.

提交回复
热议问题