Should hash equality ignore default values ?

Discussion in 'Ruby' started by Tomasz Wegrzanowski, May 7, 2007.

  1. Hash equality right now ignores the default values.
    So a == b may be true even if a[0] == b[0] would be false.

    Is this behaviour intended ?

    irb(main):001:0> a = Hash.new(0)
    => {}
    irb(main):002:0> b = Hash.new(42)
    => {}
    irb(main):003:0> a == b
    => true
    irb(main):004:0> a[0] == b[0]
    => false
    irb(main):005:0> c = Hash.new{|ht,k| ht[k] = 5}
    => {}
    irb(main):006:0> a == c
    => true
    irb(main):007:0> a[0] == c[0]
    => false


    --
    Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]
     
    Tomasz Wegrzanowski, May 7, 2007
    #1
    1. Advertising

  2. Tomasz Wegrzanowski

    Robert Dober Guest

    On 5/7/07, Tomasz Wegrzanowski <> wrote:
    > Hash equality right now ignores the default values.
    > So a == b may be true even if a[0] == b[0] would be false.
    >
    > Is this behaviour intended ?
    >
    > irb(main):001:0> a = Hash.new(0)
    > => {}
    > irb(main):002:0> b = Hash.new(42)
    > => {}
    > irb(main):003:0> a == b
    > => true
    > irb(main):004:0> a[0] == b[0]
    > => false
    > irb(main):005:0> c = Hash.new{|ht,k| ht[k] = 5}
    > => {}
    > irb(main):006:0> a == c
    > => true
    > irb(main):007:0> a[0] == c[0]
    > => false
    >
    >
    > --
    > Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]
    >
    >

    Good question Tomasz and furthermore....
    irb(main):006:0> h=Hash.new{|h,k|h[k]=1}
    => {}
    irb(main):007:0> g=Hash.new{|h,k|h[k]=2}
    => {}
    irb(main):008:0> h==g
    => true
    irb(main):009:0> h[1] ### We just must not forget that this is an assignment!!!
    => 1
    irb(main):010:0> h==g
    => false

    The second result of the comparision is clear h== {1 =>1} and g=={}

    although this might be surprising I think it is about ok, without this
    we could not compare to literals...
    tough call though
    I am eager to hear other opinions.

    Robert

    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
     
    Robert Dober, May 8, 2007
    #2
    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. Ethan Furman

    hash values and equality

    Ethan Furman, May 20, 2011, in forum: Python
    Replies:
    16
    Views:
    380
    Steven D'Aprano
    May 22, 2011
  2. Rob Meade
    Replies:
    6
    Views:
    268
    Rob Meade
    Mar 1, 2004
  3. rp
    Replies:
    1
    Views:
    537
    red floyd
    Nov 10, 2011
  4. James
    Replies:
    2
    Views:
    133
    James
    Mar 18, 2009
  5. Tim McDaniel

    Hash key types and equality of hash keys

    Tim McDaniel, Mar 1, 2012, in forum: Perl Misc
    Replies:
    2
    Views:
    794
    Tim McDaniel
    Mar 1, 2012
Loading...

Share This Page