Compare the Content, Not the Results, of Procs

后端 未结 2 491
后悔当初
后悔当初 2021-01-13 18:32

Using Ruby 1.9.2

Problem
Compare the content, not the results, of two procs. I understand the results can\'t be tested because

相关标签:
2条回答
  • 2021-01-13 18:47

    If you're using Ruby 1.9, you may be able to use the sourcify gem.

    $ irb
    > require 'sourcify'
    => true 
    > a = proc {@x == "x"}
    => #<Proc:0x9ba4240@(irb):2> 
    > b = proc {@x == %{x}}
    => #<Proc:0x9ba23f0@(irb):3> 
    > a == b
    => false 
    > a.to_source == b.to_source
    => true 
    > RUBY_VERSION
    => "1.9.2" 
    

    We also ran into the ParseTree/Ruby 1.9 incompatibility problem at my company.

    0 讨论(0)
  • 2021-01-13 18:49
    $ sudo gem install ruby2ruby ParseTree
    
    require 'parse_tree'
    require 'ruby2ruby'
    require 'parse_tree_extensions'
    
    # All of these are the same:
    proc     { puts 'a'  }.to_ruby  # => "proc { puts(\"a\") }"
    lambda   { puts "a"  }.to_ruby  # => "proc { puts(\"a\") }"
    Proc.new { puts %{a} }.to_ruby  # => "proc { puts(\"a\") }"
    
    # If you need to do this with classes:
    
    class Bar; define_method(:foo) { 'a' }; end  
    
    puts Ruby2Ruby.new.process(Unifier.new.process(ParseTree.translate(Bar)))
    
    # will print this:
    # class Bar < Object
    #   def foo
    #     "a"
    #   end
    # end
    
    0 讨论(0)
提交回复
热议问题