Proc#hash returns different values for same body

0 views
Skip to first unread message

Wolfgang Nádasi-Donner

unread,
Nov 14, 2007, 11:29:27 AM11/14/07
to Ruby-Core
Hi!

I recognized a behaviour of Proc#hash that does not follow the description.

C:\Dokumente und Einstellungen\wolfgang>ri19 Proc#hash
-------------------------------------------------------------- Proc#hash
prc.hash => integer
------------------------------------------------------------------------
Return hash value corresponding to proc body.

C:\Dokumente und Einstellungen\wolfgang>irb19
irb(main):001:0> proc{}.hash
=> 4858639
irb(main):002:0> proc{}.hash
=> 5009407
irb(main):003:0> proc{42}.hash
=> 7717959
irb(main):004:0> proc{42}.hash
=> 36991
irb(main):005:0> proc{nil}.hash
=> 80887
irb(main):006:0> proc{nil}.hash
=> 419431
irb(main):007:0> exit

C:\Dokumente und Einstellungen\wolfgang>ruby19 -v
ruby 1.9.0 (2007-11-13 patchlevel 0) [i386-mingw32]

From my understanding it should return the same value for input lines 1 and 2,
for 3 and 4, and finally for 5 and 6.

Did I understand something wrong or is this behavior a fault?

Wolfgang Nádasi-Donner

David Flanagan

unread,
Nov 14, 2007, 1:27:30 PM11/14/07
to ruby...@ruby-lang.org
The hash method works consistently with ==, but both have misleading
documentation. I posted about Proc.== on this list back in June. See
ruby-core #11401. I would update the documentation for both methods,
except that Nobu (on that thread) said that the equality behavior was
still subject to change and that procs might become comparable).

David

Wolfgang Nádasi-Donner

unread,
Nov 14, 2007, 1:30:16 PM11/14/07
to ruby...@ruby-lang.org
I made another comparison with Ruby 1.8...

C:\Dokumente und Einstellungen\wolfgang>ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-mswin32]

C:\Dokumente und Einstellungen\wolfgang>irb
irb(main):001:0> p = lambda{}
=> #<Proc:0x00000000@(irb):1>
irb(main):002:0> proc(&p).hash
=> 22169910
irb(main):003:0> proc(&p).hash
=> 22159870
irb(main):004:0> proc(&p).hash
=> 22633070
irb(main):005:0> proc(&p).hash
=> 22622090
irb(main):006:0> exit

C:\Dokumente und Einstellungen\wolfgang>ruby19 -v
ruby 1.9.0 (2007-11-13 patchlevel 0) [i386-mingw32]

C:\Dokumente und Einstellungen\wolfgang>irb19
irb(main):001:0> p = lambda{}
=> #<Proc:0x9935a0@(irb):1 (lambda)>
irb(main):002:0> proc(&p).hash
=> 4834015
irb(main):003:0> proc(&p).hash
=> 4834015
irb(main):004:0> proc(&p).hash
=> 4834015
irb(main):005:0> exit

...and would like to give the interpretation, that Proc#hash returns in Ruby 1.9
the same hash code, if the same block is used in form of a proc object. In Ruby
1.8 this is not the case.

Is this somehow (~English phrasing problems) the correct interpretation? - If
"yes" then the "ri" result together with the example in
http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l165 is misleading.

Wolfgang Nádasi-Donner

Wolfgang Nádasi-Donner

unread,
Nov 14, 2007, 1:53:59 PM11/14/07
to ruby...@ruby-lang.org
David Flanagan schrieb:

> I posted about Proc.== on this list back in June. See
> ruby-core #11401.
Is read the discussion in http://www.ruby-forum.com/topic/110696

> I would update the documentation for both methods,...
It is misleading, indeed.


> except that Nobu (on that thread) said that the equality behavior was
> still subject to change and that procs might become comparable).

It would be nice to compare Proc objects on basis of their block contents (the
original written text or a derived tree) and the binding, but I see there some
problems...

Wolfgang Nádasi-Donner

Yukihiro Matsumoto

unread,
Nov 14, 2007, 9:45:21 PM11/14/07
to ruby...@ruby-lang.org
Hi,

In message "Re: Proc#hash returns different values for same body"


on Thu, 15 Nov 2007 03:30:16 +0900, Wolfgang Nádasi-Donner <ed.o...@wonado.de> writes:

|...and would like to give the interpretation, that Proc#hash returns in Ruby 1.9
|the same hash code, if the same block is used in form of a proc object. In Ruby
|1.8 this is not the case.

In 1.8, &proc pushes copy of the proc due to implementation issues.

matz.

Rick DeNatale

unread,
Nov 15, 2007, 3:10:49 PM11/15/07
to ruby...@ruby-lang.org

I would think that for two procs to be considered equal, not only the
body but the bindings would need to be the same.

Consider

def proc1
a = 1
Proc.new {a}
end

def proc2
a = 2
Proc.new {a}
end

The bodies of the procs returned by these methods are the same, at
least textually, but the defininely are not equal in terms of effect.
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Reply all
Reply to author
Forward
0 new messages