B Seven
B Seven

Reputation: 45943

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

p = Proc.new{ puts 'ok' }

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

inspect returns the memory location:

puts p.inspect
#<Proc:0x007f9e42980b88@(irb):2>

Ruby 1.9.3

Upvotes: 15

Views: 10420

Answers (5)

Zalom
Zalom

Reputation: 754

Although an old question, still, I wanted to share my thoughts.

You can use Pry gem and end up with something like this:

[11] pry> p = Proc.new{ puts 'ok' }
=> #<Proc:0x007febe00e6360@(pry):23>

[12] pry> show-source p

From: (pry)
Number of lines: 1

p = Proc.new{ puts 'ok' }

Also, if you would use it from Rails context, you can put:

::Kernel.binding.pry

in your controllers or models, and

- require 'pry'; binding.pry

in your views, where you want to start debugging.

And in the tests, I use a combination, first require 'pry' at the top, and then ::Kernel.binding.pry where needed.

References:

Upvotes: 2

Shimaa Marzouk
Shimaa Marzouk

Reputation: 467

If proc is defined into a file, U can get the file location of proc then serialize it, then after deserialize use the location to get back to the proc again

proc_location_array = proc.source_location

after deserialize:

file_name = proc_location_array[0]

line_number = proc_location_array[1]

proc_line_code = IO.readlines(file_name)[line_number - 1]

proc_hash_string = proc_line_code[proc_line_code.index("{")..proc_line_code.length]

proc = eval("lambda #{proc_hash_string}")

Upvotes: 1

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369458

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.

Upvotes: 5

David Unric
David Unric

Reputation: 7719

Do you mean the original source code or its bytecode representation ?

For the former you may use standard Proc's method source_location

p.source_location
=> ["test.rb", 21]

and read the appropriate lines of code.

For the latter it may come handy the RubyVM::InstructionSequence and its class method disassemble:

irb> RubyVM::InstructionSequence.disasm p
=> "== disasm: <RubyVM::InstructionSequence:block in irb_binding@(irb)>
=====\n== catch table\n| catch type: redo   st: 0000 ed: 0011 sp: 0000
cont: 0000\n| catch type: next   st: 0000 ed: 0011 sp: 0000 cont:
0011\n|------------------------------------------------------------------------\n
0000 trace            1                                               
(   1)\n0002 putself          \n0003 putstring        \"ok\"\n0005 
send             :puts, 1, nil, 8, <ic:0>\n0011 leave            \n"

Upvotes: 9

Stefan
Stefan

Reputation: 114178

Take a look at the sourcify gem:

proc { x + y }.to_source
# >> "proc { (x + y) }"

Upvotes: 10

Related Questions