[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Isn't hash default value behavior just a little bizarre???

Farhad Farzaneh

7/4/2007 7:20:00 AM

Try this:

>> x = Hash.new([])
=> {}
>> x[:test]
=> []
>> x[:test] << 'this'
=> ["this"]
>> x[:bar]
=> ["this"]

Huh? It seems that when I index a hash with a new key, it returns the
Hash's default value object, not a copy of it. So if you modify that,
it modifies the default value, which then effects the default value for
every other new key.

I don't know, I wouldn't call this the "principle of least surprise"...
I was plenty surprised!

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

8 Answers

Farhad Farzaneh

7/4/2007 7:46:00 AM

0

Peña, Botp wrote:

>
> ruby allows one to change the default value. that is good, imho.
> i think the surprise stems fr the fact that it is _too simple to change
> the default. hey, it's ruby ;)
>
> kind regards -botp

You're right. Somehow I missed that in the documentation.... did RTFM,
but obviously not carefully enough.

Still, I do find it pretty surprising. I got stung when I was actually
using the feature to initialize any entry to an array just to avoid the
business of

hash[key] ||= []

Lesson learned.

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

Peña, Botp

7/4/2007 8:25:00 AM

0

On Behalf Of Farhad Farzaneh:
# Still, I do find it pretty surprising. I got stung when I
# was actually
# using the feature to initialize any entry to an array just to
# avoid the
# business of
#
# hash[key] ||= []

just be careful w hash#[]<< method (but not too careful, otherwise, you would not enjoy ruby :). What you really wanted then was hash#[]=

irb(main):041:0> y = Hash.new
=> {}
irb(main):043:0> y[1]
=> nil
irb(main):044:0> y[1] << "test"
NoMethodError: undefined method `<<' for nil:NilClass
from (irb):44
from :0

here, since y.default == nil or y[1] == nil, we are actually doing nil << "test" wc is obviously wrong.

irb(main):045:0> y[1] = "test"
=> "test"

take a look also at hash#default

Alex Young

7/4/2007 9:14:00 AM

0

Farhad Farzaneh wrote:
> Peña, Botp wrote:
>
>> ruby allows one to change the default value. that is good, imho.
>> i think the surprise stems fr the fact that it is _too simple to change
>> the default. hey, it's ruby ;)
>>
>> kind regards -botp
>
> You're right. Somehow I missed that in the documentation.... did RTFM,
> but obviously not carefully enough.
>
> Still, I do find it pretty surprising. I got stung when I was actually
> using the feature to initialize any entry to an array just to avoid the
> business of
>
> hash[key] ||= []
>
Apologies if you've already come across this, but I think the correct
way to do what you're trying to do is this:

h = Hash.new(){|h,k| h[k] = []}

The block passed to Hash.new gets called for each new entry in the hash,
so a new array will be constructed each time.

Hope this helps,
--
Alex

Farhad Farzaneh

7/4/2007 9:22:00 AM

0

Alex Young wrote:
> Apologies if you've already come across this, but I think the correct
> way to do what you're trying to do is this:
>
> h = Hash.new(){|h,k| h[k] = []}
>
> The block passed to Hash.new gets called for each new entry in the hash,
> so a new array will be constructed each time.
>
> Hope this helps,

Sweet. Now that looks very Ruby'ish! Thanks.

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

dblack

7/4/2007 11:10:00 AM

0

Farhad Farzaneh

7/4/2007 4:43:00 PM

0

unknown wrote:
> Hi --
>
>
> All you're doing is assigning an object to a certain role. There's no
> implication of automatic duplication of the object.

I was thinking of it as initialization of the value of the item
accessed, but when you think of it as *the* default object it makes
sense.

>
>> I got stung when I was actually
>> using the feature to initialize any entry to an array just to avoid the
>> business of
>>
>> hash[key] ||= []
>
> It doesn't do that anyway:
>
> h = {}
> h.default = []
> p h["blah"] # [] (default value for undefined key)
> p h # {} (no keys are defined)
>
> The default value is for undefined keys, whereas hash[key] ||= []
> actually sets a key.
>

The misunderstanding on my part was that I thought it initialized the
key to the default object, rather than returned the actual default
object. In this context, if the key is automatically initialized to an
empty array, then when I wanted to assign something to it, I wouldn't
have to first make it an array.


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

Just Another Victim of the Ambient Morality

7/5/2007 12:59:00 AM

0


"Farhad Farzaneh" <ff@onebeat.com> wrote in message
news:3dd488801d827f38a1c8f125402e9451@ruby-forum.com...
> Peña, Botp wrote:
>
>>
>> ruby allows one to change the default value. that is good, imho.
>> i think the surprise stems fr the fact that it is _too simple to change
>> the default. hey, it's ruby ;)
>>
>> kind regards -botp
>
> You're right. Somehow I missed that in the documentation.... did RTFM,
> but obviously not carefully enough.
>
> Still, I do find it pretty surprising. I got stung when I was actually
> using the feature to initialize any entry to an array just to avoid the
> business of
>
> hash[key] ||= []
>
> Lesson learned.

I don't recall Ruby ever sacrificing power for Principle of Least
Surprise (indeed, it's arguable that this isn't even one of Ruby's design
goals...).
In particular, there are times (although not common) where you do want
the default value to be shared across key entries, in which case, Ruby
currently allows you to do so. Suppose Ruby behaved as you expected. How
would you do this? I think you'll quickly see that you can't and you will,
thus, see why Ruby does what it does...



Farhad Farzaneh

7/5/2007 2:42:00 PM

0

Logan Capaldo wrote:
> On 7/4/07, Farhad Farzaneh <ff@onebeat.com> wrote:
>> => ["this"]
>> Posted via http://www.ruby-....
>>
>>
> Copying is an interesting thing. For many objects there is a sane,
> obvious
> way to make a copy. But for many others it is not so clear. Consider for
> instance, Hash.new($stdin). What should it mean, in this context, to
> "copy"
> $stdin? Or consider Hash.new(some_very_large_object). Do you want a copy
> all
> the time? Luckily, Hash does have a mechanism to acheive what you want:
>
>>> x = Hash.new { |h,k| h[k] = [] }
> => {}
>>> x[:test]
> => []
>>> x[:test] << 'this'
> => ["this"]
>>> x[:bar]
> => []
>
> POLS also is matz.'s POLS.

Current behavior does (of course) make sense, and my confusion was due
to my misunderstanding of the feature and its intended use.

Thanks to all.



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