From: George Ogata Date: 2007-02-13T11:59:37+09:00 Subject: Re: What is Proc#== Hi Kalman, Sylvain, list, On 2/13/07, Kalman Noel wrote: > Sylvain Joyeux: > > From the description, > > ---------------------------------------------------------------- Proc#== > > prc == other_proc => true or false > > ------------------------------------------------------------------------ > > Return true if prc is the same object as other_proc, or if they > > are both procs with the same body. > > > > I thought that a == b in > > ============================================= > > def block_to_proc(&prc) > > prc > > end > > def test > > block_to_proc do > > end > > end > > > > a = test > > b = test > > My guess would be that a and b were created in different contexts, and > that this is why they are not equal. The problem about that is that thay > aren't really created in different contexts here. They are in this case, > however: > > def test(arg) > block_to_proc { arg } > end > > a = test(1) > b = test(2) > > Obviously a and b are not equal here, because a.call and b.call are not > equal. Note, also: > > x = 3 > lambda { } == lambda { } # => true > lambda { x } == lambda { x } # => false (maybe because x may have changed > in between) Actually, since blocks are closures, the x is the same variable in that line. If the next line was x = 5, then the x in both would be updated. (Well, unless those blocks had been GC'ed by then... ;-) > Still the behavior you described seems like a bug to me, because of > lambda { } == lambda { }. But I may not see the existing reason why it > isn't. I believe the "same body" part means that the body was constructed from the same block. e.g.: body = lambda{1+2} lambda(&body) == lambda(&body) #=> true Or: def foo Proc.new == Proc.new end foo{1+2} #=> true There is one exception: empty procs are the "same" no matter how they're constructed: lambda{} == lambda{} #=> true As opposed to: lambda{nil} == lambda{nil} #=> false I agree that the rdoc is a little misleading. Perhaps an example should follow. Regards, George.