UnboundMethod#hash apparently broken in 1.8.1

Discussion in 'Ruby' started by Robert Klemme, Feb 27, 2004.

  1. Hi all,

    one can't use UnboundMethod as a hash key:

    irb(main):001:0> class Foo; def bar;end; end
    => nil
    irb(main):002:0> Foo.instance_method( :bar )
    => #<UnboundMethod: Foo#bar>
    irb(main):003:0> a = Foo.instance_method( :bar )
    => #<UnboundMethod: Foo#bar>
    irb(main):004:0> b = Foo.instance_method( :bar )
    => #<UnboundMethod: Foo#bar>
    irb(main):005:0> a == b
    => true
    irb(main):006:0> b == a
    => true
    irb(main):007:0> a.hash == b.hash
    => false
    irb(main):008:0> a.id
    => 135052720
    irb(main):009:0> a.hash
    => 135052720
    irb(main):010:0> b.id
    => 135044800
    irb(main):011:0> b.hash
    => 135044800

    UnboundMethod#hash is broken in
    $ ruby --version
    ruby 1.8.1 (2003-12-25) [i386-cygwin]

    Is that fixed in a later version?

    Kind regards

    robert
    Robert Klemme, Feb 27, 2004
    #1
    1. Advertising

  2. Hi --

    "Robert Klemme" <> writes:

    > Hi all,
    >
    > one can't use UnboundMethod as a hash key:
    >
    > irb(main):001:0> class Foo; def bar;end; end
    > => nil
    > irb(main):002:0> Foo.instance_method( :bar )
    > => #<UnboundMethod: Foo#bar>
    > irb(main):003:0> a = Foo.instance_method( :bar )
    > => #<UnboundMethod: Foo#bar>
    > irb(main):004:0> b = Foo.instance_method( :bar )
    > => #<UnboundMethod: Foo#bar>
    > irb(main):005:0> a == b
    > => true
    > irb(main):006:0> b == a
    > => true
    > irb(main):007:0> a.hash == b.hash
    > => false
    > irb(main):008:0> a.id
    > => 135052720
    > irb(main):009:0> a.hash
    > => 135052720
    > irb(main):010:0> b.id
    > => 135044800
    > irb(main):011:0> b.hash
    > => 135044800
    >
    > UnboundMethod#hash is broken in
    > $ ruby --version
    > ruby 1.8.1 (2003-12-25) [i386-cygwin]
    >
    > Is that fixed in a later version?


    I'm not sure why you describe it as broken. Can you explain
    a little further?


    David

    --
    David A. Black
    David Alan Black, Feb 28, 2004
    #2
    1. Advertising

  3. In article <>, David Alan Black wrote:
    >Hi --
    >
    >"Robert Klemme" <> writes:
    >
    >> Hi all,
    >>
    >> one can't use UnboundMethod as a hash key:
    >>
    >> irb(main):001:0> class Foo; def bar;end; end
    >> => nil
    >> irb(main):002:0> Foo.instance_method( :bar )
    >> => #<UnboundMethod: Foo#bar>
    >> irb(main):003:0> a = Foo.instance_method( :bar )
    >> => #<UnboundMethod: Foo#bar>
    >> irb(main):004:0> b = Foo.instance_method( :bar )
    >> => #<UnboundMethod: Foo#bar>
    >> irb(main):005:0> a == b
    >> => true
    >> irb(main):006:0> b == a
    >> => true
    >> irb(main):007:0> a.hash == b.hash
    >> => false
    >> irb(main):008:0> a.id
    >> => 135052720
    >> irb(main):009:0> a.hash
    >> => 135052720
    >> irb(main):010:0> b.id
    >> => 135044800
    >> irb(main):011:0> b.hash
    >> => 135044800
    >>
    >> UnboundMethod#hash is broken in
    >> $ ruby --version
    >> ruby 1.8.1 (2003-12-25) [i386-cygwin]
    >>
    >> Is that fixed in a later version?

    >
    >I'm not sure why you describe it as broken. Can you explain
    >a little further?


    I think he means because "a == b" but "!a.eql?(b)" and "a.hash != b.hash".
    It's not a bug, just a lack of a feature.

    I posted a similar message today about Sets.

    --
    Tim Sutherland <>
    2004 SDKACM President
    Software Developers' Klub - the University of Auckland ACM Student Chapter
    http://www.sdkacm.com/
    Tim Sutherland, Feb 29, 2004
    #3
  4. "Tim Sutherland" <> schrieb im Newsbeitrag
    news:...
    > In article <>, David Alan Black wrote:
    > >Hi --
    > >
    > >"Robert Klemme" <> writes:
    > >
    > >> Hi all,
    > >>
    > >> one can't use UnboundMethod as a hash key:
    > >>
    > >> irb(main):001:0> class Foo; def bar;end; end
    > >> => nil
    > >> irb(main):002:0> Foo.instance_method( :bar )
    > >> => #<UnboundMethod: Foo#bar>
    > >> irb(main):003:0> a = Foo.instance_method( :bar )
    > >> => #<UnboundMethod: Foo#bar>
    > >> irb(main):004:0> b = Foo.instance_method( :bar )
    > >> => #<UnboundMethod: Foo#bar>
    > >> irb(main):005:0> a == b
    > >> => true
    > >> irb(main):006:0> b == a
    > >> => true
    > >> irb(main):007:0> a.hash == b.hash
    > >> => false
    > >> irb(main):008:0> a.id
    > >> => 135052720
    > >> irb(main):009:0> a.hash
    > >> => 135052720
    > >> irb(main):010:0> b.id
    > >> => 135044800
    > >> irb(main):011:0> b.hash
    > >> => 135044800
    > >>
    > >> UnboundMethod#hash is broken in
    > >> $ ruby --version
    > >> ruby 1.8.1 (2003-12-25) [i386-cygwin]
    > >>
    > >> Is that fixed in a later version?

    > >
    > >I'm not sure why you describe it as broken. Can you explain
    > >a little further?

    >
    > I think he means because "a == b" but "!a.eql?(b)" and "a.hash != b.hash".


    Exactly.

    > It's not a bug, just a lack of a feature.


    I regard it rather as a bug than a lack of a feature - but I'd agree that
    it's debatable. My point is that you can't use UnboundMethod as hash key
    because of this although it would make pretty much sense. (See the other
    thread about attaching meta data to methods and classes)

    Regards

    robert
    Robert Klemme, Feb 29, 2004
    #4
  5. Robert Klemme

    Mark Hubbart Guest

    On Mar 1, 2004, at 5:14 AM, Tim Sutherland wrote:
    >> I'm not sure why you describe it as broken. Can you explain
    >> a little further?

    >
    > I think he means because "a == b" but "!a.eql?(b)" and "a.hash !=
    > b.hash".
    > It's not a bug, just a lack of a feature.


    irb(main):006:0> class Foo;def bar;end;end
    => nil
    irb(main):007:0> Foo.instance_method:)bar).hash
    => 187786
    irb(main):008:0> Foo.instance_method:)bar).hash
    => 170366
    irb(main):009:0> Foo.instance_method:)bar).hash
    => 155876
    irb(main):010:0> Foo.instance_method:)bar).hash
    => 139386

    To me, at least, this behavior is unexpected. I would expect it to
    return the same object each time; or at least objects with the same
    hash value.

    --Mark
    Mark Hubbart, Mar 1, 2004
    #5
  6. Hi,

    In message "Re: UnboundMethod#hash apparently broken in 1.8.1"
    on 04/03/02, Mark Hubbart <> writes:

    |To me, at least, this behavior is unexpected. I would expect it to
    |return the same object each time; or at least objects with the same
    |hash value.

    I think you're expecting too much. ;-)
    I'm curious why you expect so.

    matz.
    Yukihiro Matsumoto, Mar 1, 2004
    #6
  7. "Yukihiro Matsumoto" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > In message "Re: UnboundMethod#hash apparently broken in 1.8.1"
    > on 04/03/02, Mark Hubbart <> writes:
    >
    > |To me, at least, this behavior is unexpected. I would expect it to
    > |return the same object each time; or at least objects with the same
    > |hash value.
    >
    > I think you're expecting too much. ;-)
    > I'm curious why you expect so.


    At least both UnboundMethod instances refer the same instance method, i.e.
    the same thing if you like. So if put into a Set or Hash (as key) I'd
    expect them to be treated as equivalent. Is there a reason why they
    shouldn't? Can they be in a state where they no longer refer the same
    method? AFAIK UnboundMethod#bind returns a new instance:

    irb(main):001:0> class Foo;def bar;end;end
    => nil
    irb(main):002:0> m=Foo.instance_method :bar
    => #<UnboundMethod: Foo#bar>
    irb(main):003:0> f=Foo.new
    => #<Foo:0x1018b4a0>
    irb(main):004:0> x = m.bind f
    => #<Method: Foo#bar>
    irb(main):005:0> m
    => #<UnboundMethod: Foo#bar>
    irb(main):006:0> m.id
    => 135031972
    irb(main):007:0> x.id
    => 135021280
    irb(main):008:0>

    Kind regards

    robert
    Robert Klemme, Mar 2, 2004
    #7
  8. Robert Klemme

    Mark Hubbart Guest

    On Mar 1, 2004, at 3:57 PM, Yukihiro Matsumoto wrote:

    > Hi,
    >
    > In message "Re: UnboundMethod#hash apparently broken in 1.8.1"
    > on 04/03/02, Mark Hubbart <> writes:
    >
    > |To me, at least, this behavior is unexpected. I would expect it to
    > |return the same object each time; or at least objects with the same
    > |hash value.
    >
    > I think you're expecting too much. ;-)
    > I'm curious why you expect so.


    I would expect it to work that way because both UnboundMethods are
    pointing at the same exact method. Perhaps I am expecting too much,
    since I don't know how it works internally. But, from the outside, I
    expected it to essentially make another reference to that method's
    object and pass it back.

    I'm guessing (from your reaction) that what actually happens behind the
    scenes when you retrieve an UnboundMethod that way is entirely
    different from what I had assumed? If, in retrieving the UnboundMethod
    object, it creates a new wrapper object, and passes that back, it would
    make perfect sense that the hashes and ids wouldn't match. Even so, I
    wish they would match :)

    Of course, I'm only inferring, so I could easily be way way out in left
    field here.

    --Mark
    Mark Hubbart, Mar 2, 2004
    #8
  9. Hi,

    In message "Re: UnboundMethod#hash apparently broken in 1.8.1"
    on 04/03/03, "Its Me" <> writes:

    |Imho, they should be the same object. Why should
    | Foo.instance_method :)bar).id
    | Foo.instance_method:)bar).id
    |return two different ids?

    Why not? The UnboundMethod object is a wrapper of the internal "real"
    UnboundMethod. I thought UnboundMethod is not THAT important to pay
    the price to satisfy your expectation.

    |What if I want to extend class UnboundMethod so that I can add custom
    |meta-data to UnboundMethod objects?
    |
    | Foo.instance_method:)bar).arg_list = [:arg1, :arg2, :arg3]

    They don't have same id (for these are different Ruby object), but
    they are referring the same unbound method.

    matz.
    Yukihiro Matsumoto, Mar 2, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  2. gabriele renzi

    Help with UnboundMethod#bind error

    gabriele renzi, Jun 29, 2003, in forum: Ruby
    Replies:
    3
    Views:
    123
    Yukihiro Matsumoto
    Jun 30, 2003
  3. Nathaniel Talbott
    Replies:
    0
    Views:
    91
    Nathaniel Talbott
    Feb 28, 2004
  4. James Herdman

    Confusion About UnboundMethod

    James Herdman, Apr 16, 2006, in forum: Ruby
    Replies:
    1
    Views:
    82
    Robert Klemme
    Apr 16, 2006
  5. Replies:
    44
    Views:
    335
    Hal Fulton
    Aug 23, 2006
Loading...

Share This Page