But you can setup almost-autovivification if you want it, however it's not as transparent as in Perl. The Hash.new method can take a block which is passed the instance and the missing key when you try and access a non-existent key. So if you wanted to have it autovivify with an Array instance you do something like:

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

Similarly Array.new has a block initializer which will let you pre-fill it with a computed value (although it is missing something analogous to the Hash version mentioned above; then again there's nothing to stop you from extending the builtin Array class to do so either; in fact you might even could use reflection to see if the block takes one or two arguments and emulate the old behavior as well).

Update: And yes, this is very different from the Perl case. Ruby is relying on explicit instructions from the programmer rather than syntactic hints (which I think is what you were getting at, rather than saying you absolutely can't do it in Ruby).

--
We're looking for people in ATL


In reply to Re^4: Perl 5's greatest limitation is...? by Fletch
in thread Perl 5's greatest limitation is...? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.