[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Hashes

Tim Wolak

4/22/2008 8:09:00 PM


> I didn't realize this before, but even if it's off topic, I thought
> I'd bring it up.
>
> Hashes -- during creation -- allow assignment more than once in 1.8.6
> with the last one taking precedence
>
> Is that somehow common knowledge that I missed in the Pickaxe? Just
> one of those little things, I guess.
>
> irb(main):002:0> h = {[1, 2, 3] => [4, 5, 6], [1, 2, 3] => [7, 8, 9]}
> => {[1, 2, 3]=>[7, 8, 9]}
>
> Todd
>
Ouch, I guess that would be a bad thing... If I'm going line by line then I
would think I would be ok then as the hash is closed after I finish to take
on the next line correct?

Hmmm nice catch Todd!


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


3 Answers

David A. Black

4/22/2008 8:12:00 PM

0

Hi --

On Wed, 23 Apr 2008, Tim Wolak wrote:

>
>> I didn't realize this before, but even if it's off topic, I thought
>> I'd bring it up.
>>
>> Hashes -- during creation -- allow assignment more than once in 1.8.6
>> with the last one taking precedence
>>
>> Is that somehow common knowledge that I missed in the Pickaxe? Just
>> one of those little things, I guess.
>>
>> irb(main):002:0> h = {[1, 2, 3] => [4, 5, 6], [1, 2, 3] => [7, 8, 9]}
>> => {[1, 2, 3]=>[7, 8, 9]}
>>
>> Todd
>>
> Ouch, I guess that would be a bad thing... If I'm going line by line then I
> would think I would be ok then as the hash is closed after I finish to take
> on the next line correct?

It's never exactly closed, in that sense. Hash keys are unique, so if
you key a new value, you lose the old value, even if it's not at the
time that the hash is created.

If your keys are account numbers and your values are arrays of
balances, you'll be all right as long as you don't trample the arrays
-- just add to them.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Todd Benson

4/22/2008 8:39:00 PM

0

On Tue, Apr 22, 2008 at 3:12 PM, David A. Black <dblack@rubypal.com> wrote:

>
> It's never exactly closed, in that sense. Hash keys are unique, so if
> you key a new value, you lose the old value, even if it's not at the
> time that the hash is created.

I guess I kind of knew that, but I was a tiny bit surprised this
happened in the Hash creation without throwing something like a "key
exists" exception. It doesn't bother me. In fact, I think I can make
some use of it.

Todd

Todd Benson

4/22/2008 9:08:00 PM

0

On Tue, Apr 22, 2008 at 3:08 PM, Tim Wolak <twolak@sktydev.com> wrote:
>
> > I didn't realize this before, but even if it's off topic, I thought
> > I'd bring it up.
> >
> > Hashes -- during creation -- allow assignment more than once in 1.8.6
> > with the last one taking precedence
> >
> > Is that somehow common knowledge that I missed in the Pickaxe? Just
> > one of those little things, I guess.
> >
> > irb(main):002:0> h = {[1, 2, 3] => [4, 5, 6], [1, 2, 3] => [7, 8, 9]}
> > => {[1, 2, 3]=>[7, 8, 9]}
> >
> > Todd
> >
> Ouch, I guess that would be a bad thing... If I'm going line by line then I
> would think I would be ok then as the hash is closed after I finish to take
> on the next line correct?
>
> Hmmm nice catch Todd!

Hi Tim,

I didn't have a close look at your original post, but in all honesty
you should probably listen to David. I just brought up a surprise in
Hash creation that I didn't expect. I don't think it would affect
your code.

Todd