How is Proc#== evaluated? RDoc says:
prc == other_proc → true or false
Returns true if prc is the same object as other_proc, or if they are both procs with the same body.
But it is not clear what counts as having "the same body". One condition seems to be that the arity must be the same:
->{} == ->{} # => true
->{} == ->x{} # => false
->x{} == ->x{} # => true
->x{} == ->y{} # => true
->x{} == ->y,z{} # => false
But there is more than that. As RDoc says, the body matters:
->{nil} == ->{nil} # => true
->{nil} == ->{false} # => false
->{false} == ->{false} # => true
But at the same time, it looks like the proc is not fully evaluated:
->{} == ->{nil} # => false
->{false} == ->{1 == 2} # => false
To what extent is the body evaluated?
This has changed in Ruby 2.0, so you should not try to compare
Procs. They won’t be==unless they are exactly the same object.The discussion can be found here.
If you really need to compare the code of two blocks and are using MRI, you can play around with
RubyVM::InstructionSequence.disassemble(block), or even better in Ruby 2.0RubyVM::InstructionSequence.of(block).