Printing the source code of a Ruby block

前端 未结 4 1040
情书的邮戳
情书的邮戳 2020-12-06 01:33

I have a method that takes a block.

Obviously I don\'t know what is going to be passed in and for bizarre reasons that I won\'t go into here I want to print the cont

4条回答
  •  情书的邮戳
    2020-12-06 02:30

    Building on Evangenieur's answer, here's Corban's answer if you had Ruby 1.9:

    # Works with Ruby 1.9
    require 'sourcify'
    
    def meth &block
      # Note it's to_source, not to_ruby
      puts block.to_source
    end
    
    meth { some code }
    

    My company uses this to display the Ruby code used to make carbon calculations... we used ParseTree with Ruby 1.8 and now sourcify with Ruby 1.9.

提交回复
热议问题