Just Another Victim of the Ambient Morality
7/5/2007 12:59:00 AM
"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...