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

后端 未结 5 1038
别那么骄傲
别那么骄傲 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条回答
  •  一生所求
    2021-02-05 16:28

    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' }
    => #
    
    [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:

    • http://pryrepl.org
    • https://github.com/pry/pry
    • https://github.com/pry/pry/wiki/Source-browsing

提交回复
热议问题