[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

defining key equivalence in a hash

Suraj Kurapati

12/10/2006 7:33:00 PM

Hello,

I'm trying to make two keys equivalent by having their #hash method
produce the same result. But this does not seem to work:

irb(main):001:0> class K; def hash; 90; end; end
=> nil
irb(main):002:0> h = {}
=> {}
irb(main):003:0> h[K.new] = 1
=> 1
irb(main):004:0> h.key? K.new # <---- I want this to return true!
=> false

What am I missing?

Thanks for your consideration.

--
Posted via http://www.ruby-....

5 Answers

Daniel Finnie

12/10/2006 7:40:00 PM

0

If you put K.new into a variable, and then check for that, it will
return true:

irb(main):001:0> class K; def hash; 90; end; end
=> nil
irb(main):002:0> h = {}
=> {}
irb(main):003:0> k = K.new
=> #<K:0xb7cac4ac>
irb(main):004:0> h[k] = 1
=> 1
irb(main):005:0> h[k]
=> 1
irb(main):006:0> h.key? k
=> true

It's because K.new creates a new location in memory for each call to it,
but h.key? checks the location in memory, not the contents of the keys
and the objects passed to it. So if you call K.new twice, each will
return a new location in memory.

Dan


Suraj Kurapati wrote:
> Hello,
>
> I'm trying to make two keys equivalent by having their #hash method
> produce the same result. But this does not seem to work:
>
> irb(main):001:0> class K; def hash; 90; end; end
> => nil
> irb(main):002:0> h = {}
> => {}
> irb(main):003:0> h[K.new] = 1
> => 1
> irb(main):004:0> h.key? K.new # <---- I want this to return true!
> => false
>
> What am I missing?
>
> Thanks for your consideration.
>

Eric Hodel

12/10/2006 7:53:00 PM

0

On Dec 10, 2006, at 11:33 , Suraj Kurapati wrote:

> Hello,
>
> I'm trying to make two keys equivalent by having their #hash method
> produce the same result. But this does not seem to work:
>
> irb(main):001:0> class K; def hash; 90; end; end
> => nil
> irb(main):002:0> h = {}
> => {}
> irb(main):003:0> h[K.new] = 1
> => 1
> irb(main):004:0> h.key? K.new # <---- I want this to return true!
> => false
>
> What am I missing?

class K; def eql?(other) true; end; end

#eql? is called if o1.hash == o2.hash. If #eql? returns true the
objects belong to the same index.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Ara.T.Howard

12/10/2006 7:55:00 PM

0

The Revd

7/30/2012 6:54:00 PM

0

On Mon, 30 Jul 2012 09:00:00 -0700 (PDT), Michael Ejercito
<mejercit@hotmail.com> wrote:

>On Jul 30, 5:22?am, The Revd <peel...@degenerate.Grik> wrote:
>> On Mon, 30 Jul 2012 00:37:25 -0700 (PDT),Michael Ejercito
>>
>> <mejer...@hotmail.com> wrote:
>> >On Jul 29, 10:26 pm, The Revd <peel...@degenerate.Grik> wrote:
>> >> On Sun, 29 Jul 2012 18:48:39 -0700 (PDT),Michael Ejercito
>>
>> >> <mejer...@hotmail.com> wrote:
>> >> > The following Usenet
>>
>> >> <YAAAAAAAAAAAAWN>
>> > ? The following ballad
>>
>> <YAAAAAAAAAAAAAAAAAAAAAAAAAWN>
> The following ballad

<YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWN>

Michael Ejercito

7/30/2012 10:21:00 PM

0

On Jul 30, 11:54 am, The Revd <peel...@degenerate.Grik> wrote:
> On Mon, 30 Jul 2012 09:00:00 -0700 (PDT), Michael Ejercito
>
>
>
>
>
>
>
>
>
> <mejer...@hotmail.com> wrote:
> >On Jul 30, 5:22 am, The Revd <peel...@degenerate.Grik> wrote:
> >> On Mon, 30 Jul 2012 00:37:25 -0700 (PDT),Michael Ejercito
>
> >> <mejer...@hotmail.com> wrote:
> >> >   The following ballad
>
> >> <YAAAAAAAAAAAAAAAAAAAAAAAAAWN>
> >   The following ballad
>
> <YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWN>
The following ballad was dedicated to your kind, boy.

http://groups.google.com/group/soc.veterans/msg/11f6b2...

THE BALLAD OF THE ANTI-SEMITE

Sometimes they’re black sometimes they’re white,
But they’re always an asshole wanting to fight.
Well they hate the Jew for no good reason at all,
They stink, they smell, and they jack-off in the hall.
Well they’re stupid sons the bitches that they are,
They play with their dicks in the front seat of a car,
Most are Muslim and some are German Huns,
Their asses are big and fat and they look like hog buns.
Their hatred is a weakness on all mankind,
Most of them are prejudice and they seem to be blind.
They say they aren’t queer but you know they’re telling a lie,
They’re filthy and nasty and they step in fresh cowpie.
The Aryans and Muslims are anti-Semites to tell,
Both have a stink yes both do smell.
They’re not very courageous they’re pussies to be,
And they all suffer from having a little pee-pee.
Their women are ugly and look like my ass,
They stick their nose down where the dog pissed on the grass.
Well that is all I have to say,
About the anti-Semite gay.